Welcome! Log In Create A New Profile

Advanced

Edit page - Update Query

Posted by 46733590 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Edit page - Update Query
September 29, 2013 08:28PM
Hi All
I am busy with the edit page for the assignment and have run into an issue. The issue being that I cannot get the UPDATE query to function.
What I have tried.
1.I get the Player Id from the list_players page.
2.I use the select query to pull the information for the specific rows information using the player id.
3. This populates the fields in the form and will allow me to change the information in the txt box.
4. I use the server request method to make sure the update query only actions once I post from the form.
5.I get this error once I post execute --> Notice: Undefined index: player_id in C:\Program Files (x86)\EasyPHP-5.3.3\www\edit.php on line 6, Query Failed: 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 '' at line 1

Thanks All. Will appreciate any help to sort this out.
Regards,

Language: PHP
<?php //Get the Player Id that is in the URL $player_id=$_GET[';player_id';];   //DB Query using the player ID $query = "SELECT * FROM registration WHERE player_id=$player_id"; $result = mysql_query($query) or die ("Query Failed: " . mysql_error()); $row = mysql_fetch_array($result);   // get data from db $name = $row[';name';]; $surname = $row[';surname';]; $contact_number = $row[';contact_number';]; $email = $row[';email';]; $position = $row[';position';]; $username = $row[';username';]; $password = $row[';password';];   //Will action if the Request method is post, so only once you update information if ($_SERVER[';REQUEST_METHOD';] === ';POST';) { //Get the Player Id that is in the URL //$player_id=$_GET[';player_id';]; $query = "UPDATE registration SET surname=';$surname';, name=';$name';, contact_number=';$contact_number';, email=';$email';, position=';$position';, username=';$username';, password=';$password'; WHERE player_id=';$player_id';"; $result = mysql_query($query) or die ("Query Failed: " . mysql_error());     $row = mysql_fetch_array($result); echo "Successfully Updated :" .$player_id;   }   ?>
Language: HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title> Edit Players </title> </head> <center> <div style="position:absolute;top:0px;left:30%;right:30%"><a href="insert.php">Insert a Player</div> <div style="position:absolute;top:0px;left:15%;right:15%"><a href="edit.php">Edit a players Information</div> </center> <body>   <form action="edit.php" method="post"> Player ID: <?php echo $player_id; ?> </br> First Name: <input type="text" name="name" value="<?php echo $name; ?>"/><br/> Surname : <input type="text" name="surname" value="<?php echo $surname; ?>"/><br/> Contact Number : <input type="text" name="contact_number" value="<?php echo $contact_number; ?>"/><br/> Email : <input type="text" name="email" value="<?php echo $email; ?>"/><br/> Position : <input type="text" name="position" value="<?php echo $position; ?>"/><br/> User Name : <input type="text" name="username" value="<?php echo $username; ?>"/><br/> Password : <input type="text" name="password" value="<?php echo $password; ?>"/><br/>   <input type="submit" name="submit" value="Submit"> </body> </html>
avatar Re: Edit page - Update Query
September 29, 2013 10:21PM
How does the player_id get into the url if your form method is POST ?


Closing tag </form> ?


In your UPDATE statement you are simply writing back the same values that you extracted from the DB. You need to extract the edited values from $_POST.
Re: Edit page - Update Query
October 01, 2013 12:52PM
I think I know what you mean now Bigron11.

Language: PHP
$query = "UPDATE registration SET surname=';$_POST[surname]';, name=';$_POST[name]';, contact_number=';$_POST[contact_number]';, email=';$_POST[email]';, position=';$_POST[position]';, username=';$_POST[username]';, password=';$_POST[password]'; WHERE player_id=';.$player_id';";

And this work except i get an error due to line 6. Below
Language: PHP
$player_id=$_GET[';player_id';];

If I remove and edit to this it works.
Language: PHP
$player_id=1;
Please can someone explain what im doing wrong here.
Thank you,
avatar Re: Edit page - Update Query
October 01, 2013 01:09PM
Your player_id is NOT in $_GET. ($_GET is in fact not set - you should check if $_GET is set before you try to extract values from it)
You would either need to have your form method="GET", or manually attach player_id to the form action url.

The best method is probably to put your player_id into a hidden field, and then extract it from the $_POST array.

Put this in your form -

<INPUT type="hidden" name="playerid" value ="<?php ?????? ?>" >

And then in your "action" script -

$player_id = $_POST['playerid'];
avatar Re: Edit page - Update Query
October 01, 2013 01:25PM
Language: PHP
$query = "UPDATE registration SET surname=';$_POST[surname]';, name=';$_POST[name]';, contact_number=';$_POST[contact_number]';, email=';$_POST[email]';, position=';$_POST[position]';, username=';$_POST[username]';, password=';$_POST[password]'; WHERE player_id=';.$player_id';";

Your syntax is not correct.
You are missing quotes around the item names -

Language: PHP
$_POST[';email';]

It would be easier to assign the $_POST values to variables, and then use the variables in your $query statement.
Re: Edit page - Update Query
October 04, 2013 12:04PM
The owner of this student #46733590 just asked exactly what I'm struggling with. Thank u guys I now have the clear idea of what I have to do. My portfolio is almost done, I'm getting there.
Re: Edit page - Update Query
October 04, 2013 08:15PM
my query syntax that works is as follows:

Language: PHP
$query = mysql_query("UPDATE `players` SET `name` = ';$name'; WHERE `player_id` = ';$id';");

Hope that helps
Sorry, only registered users may post in this forum.

Click here to login