Jump to content

PHP & mySQL problem


fozze

Recommended Posts

this is probably going to be really simple to fix but i cant figure it out :/ on one page i have a form which uses php to populate a dropdown box, now this form will allow the user to add 1 to the person which was selected in the dropdown box.

 

The code for this looks like this..

 

 

<form action="addpoint.php" method="post">

<select>

 

<?php

$sql="SELECT id,name FROM man";

$result =mysql_query($sql);

while ($data=mysql_fetch_assoc($result)){

?>

<option value ="<?php echo $data['id'] ?>" ><?php echo $data['name'] ?></option>

<?php } ?>

</select>

<input type="submit" value="Add Point"/>

 

</form>

 

the php to process this form looks like this..

 

<?php

 

$sql="UPDATE man SET points = (points + 1) WHERE name =

('$_POST[name]')";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "$_POST[name] has been added";

 

mysql_close($con)

?>

 

 

The problem i have is that i dont think its pulling the name from the form so therefore it wont effect the database, ive tried the sql like this..

 

UPDATE man SET points = (points + 1) WHERE name = 'joe'

 

and it works fine, any help would be gratefully received :)

 

Owen

 

Link to comment
Share on other sites

First off.. you do know you can simply echo the whole form to right..? Since right now I see you using <?php ?> tags for every variable you would like to echo, which I find very annoying (although if you prefer this, feel free to do it this way)

 

About your problem.. It seems you forgot that it actually is a variable, which means you must call the query like this:

UPDATE man SET points = (points + 1) WHERE name = '".$_POST[name]."'";

 

The dots are basically the problem, when you set a variable in an echo, or query simply must always have those dots. Don't worry though, I think a lot of people forget about these things all the time =P

 

Edit: yes, select tags are required to

Link to comment
Share on other sites

About your problem.. It seems you forgot that it actually is a variable, which means you must call the query like this:

UPDATE man SET points = (points + 1) WHERE name = '".$_POST[name]."'";

 

The dots are basically the problem, when you set a variable in an echo, or query simply must always have those dots. Don't worry though, I think a lot of people forget about these things all the time =P

 

 

The string concatenation, or 'dots', is not needed, and does nothing but lead to more typo errors. The string is double quoted, and the variable will be interpolated as-is. The only change I'd make (and it technically isn't really needed), would be to quote the array index then use complex notation. I do that mainly for consistency.

 

$sql="UPDATE man SET points = ( points + 1 ) WHERE name = ( '{$_POST['name']}' )";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.