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.

Dani AI

Generated

Nice result — got the increment working and was right that the number column should be incremented rather than replaced. The thread also shows common beginner problems: using the wrong column name (id vs name), building the SQL string so it becomes syntactically invalid, and relying on the old mysql_* functions without escaping (which invites SQL injection).

For a safer, simpler production pattern use an upsert with prepared statements (PDO or mysqli). This does the insert-if-missing and increment-in-one-step, so you don't have to SELECT first. Make the name column UNIQUE (or use an integer primary id) and set number NOT NULL DEFAULT 0. Example (PDO):

$pdo = new PDO($dsn, $dbuser, $dbpass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

$stmt = $pdo->prepare(
  "INSERT INTO turf_table (name, number) VALUES (:name, 1)
   ON DUPLICATE KEY UPDATE number = number + 1"
);
$stmt->execute([':name' => $name]);

If you prefer the simple UPDATE-only route (what you ended up using), remember to:

  • Properly quote/escape the string value (or better, use prepared statements).
  • Check affected rows to detect when the row didn't exist.
  • Use InnoDB for row-level locking; number = number + 1 in a single UPDATE is atomic for concurrency.

Debugging tips from the thread: echo the SQL string from PHP to inspect it (but don’t paste a PHP echo line into phpMyAdmin), use var_dump() on the incoming $name, trim and validate input, and enable exceptions/error reporting so failures are visible. These small changes make the solution robust and secure.

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.