0

Okay. I'm a php newbie.

I have a form where the user enters their name.

I then on the next form I want it to access the database and add 1 to the row named 'number' that matches the persons name that they previously entered. If the name does not exist, I want it to create new data for that person.

Any help would be much appreciated.

4
Contributors
31
Replies
32
Views
8 Years
Discussion Span
Last Post by Shanti C
Featured Replies
  • If you got the solution then ignore this otherwise try to update the number filed by adding 1 to it where name will be equal to the selected name from database. Read More

0

if u need suggestion....

first u check weather the data is present or not.
if not then insert a new row.

0

The code would be great. I'm new to PHP and don't really know how to use it properly yet.

0
$result=mysql_query('select * from user where table='.$user_id.'');
if(mysql_num_rows($result) == 0){
$insert_user= 'insert into user set id='.$user_id.'';
mysql_query($insert_user) || die(mysql_error());
}

modify as ur enties

0

sorry forgot last enrty

$result=mysql_query('select * from tablename where id='.$user_id.'');
if(mysql_num_rows($result) == 0){
$insert_user= 'insert into tablename set id='.$user_id.'';
mysql_query($insert_user) || die(mysql_error());
}

modify as ur enties

0

Sorry, but how would I go about adding one to that persons 'number' row?

0

This is my code so far.

$name = $_REQUEST['name'];
  $set = $_REQUEST['set'];
  $email = $_REQUEST['email'];

$con = mysql_connect("XXXXXXXXXXXXXX","XXXXXXX","XXXXX");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("a6114672_dope", $con);

$result=mysql_query('select * from turf_table where id='.$name.'');
if(mysql_num_rows($result) == 0){
$insert_user= 'insert into turf_table set id='.$name.'';
mysql_query($insert_user) || die(mysql_error());
}

mysql_close($con);
0

For example, my data table is:

Name: Number:
Matthew 20


And I want to add 1 to the number in the number column.

0

add 1 to the number for the person who enters their name in the form.

0

So this should work?

$result="update turf_table set number=(number+1) where id='.$name.'";

mysql_query($result) || die(mysql_error());
0

Okay, well the code parsed with no errors, but 1 did not get added.

$name = $_REQUEST['name'];
  $set = $_REQUEST['set'];
  $email = $_REQUEST['email'];

$con = mysql_connect("xxxxxxxxxxx","xxxxxxx","xxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("a6114672_dope", $con);

$result="update turf_table set number=(number+1) where name='.$name.'";

mysql_query($result) || die(mysql_error());


mysql_close($con);
0

just echo this query like..

echo $result="update turf_table set number=(number+1) where name='.$name.'";
exit;

and copy and paste that query at your database..
tel me what you got there....

or post your table structure...

0

Error

SQL query:

echo $result = "update turf_table set number=(number+1) where name='.$name.'"

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'echo $result="update turf_table set number=(number+1) where name='.$name.'"' at line 1

0

Table Structure:

Field Type
-------------------
name varchar(50)
number int(5)

1

If you got the solution then ignore this otherwise
try to update the number filed by adding 1 to it where name will be equal to the selected name from database.

Comments
good point...
0

echo $name;
tell me what it prints....

Okay, in the text box i typed "Matthew" and then I pressed submit. On the next form echo $name answered "Matthew"... so I guess that's working correctly.

0

please confirm that name(what you typed) is same as name in database....
or post your needed code...

0

please confirm that name(what you typed) is same as name in database....
or post your needed code...

Thank you very much for your help.

I managed to solve the problem my self.

mysql_query("UPDATE turf_table SET number = (number+1)
WHERE name = '$name'");
0

Thank you very much for your help.

I managed to solve the problem my self.

tel me what your mistake????

0

tel me what your mistake????

No mistake. I just wrote my own code.

Thank you for pointing me in the right direction.

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.