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.

Recommended Answers

All 31 Replies

wat u need code or suggestion?

if u need suggestion....

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

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

$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

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

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

i think you are not complete with your posts.....

Please provide your thread clearly...

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);

For example, my data table is:

Name: Number:
Matthew 20


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

i don't understand what do you want???

you want add 1 to number or update number field with 1

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

then write qury like:

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

Ahh, thank you very much. I am testing it now.

So this should work?

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

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

yes....

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);

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...

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

Table Structure:

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

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.

commented: good point... +2

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

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.

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

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'");

Thank you very much for your help.

I managed to solve the problem my self.

tel me what your mistake????

tel me what your mistake????

No mistake. I just wrote my own code.

Thank you for pointing me in the right direction.

then make your thread solved....

then make your thread solved....

How? LOL

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.