954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Form and Database Help Please

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.

mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

wat u need code or suggestion?

nikesh.yadav
Posting Whiz in Training
219 posts since Feb 2008
Reputation Points: 15
Solved Threads: 21
 

if u need suggestion....

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

nikesh.yadav
Posting Whiz in Training
219 posts since Feb 2008
Reputation Points: 15
Solved Threads: 21
 

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

mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 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

nikesh.yadav
Posting Whiz in Training
219 posts since Feb 2008
Reputation Points: 15
Solved Threads: 21
 

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

nikesh.yadav
Posting Whiz in Training
219 posts since Feb 2008
Reputation Points: 15
Solved Threads: 21
 

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

mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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

Please provide your thread clearly...

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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);
mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

For example, my data table is:

Name: Number:
Matthew 20


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

mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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

mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

then write qury like:

$result="update turf_table set number=(number+1) where id='.$name.'";
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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

mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

So this should work?

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

mysql_query($result) || die(mysql_error());
mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

yes....

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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);
mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 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...

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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

mattyb15
Light Poster
26 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You