Hi! First of all to say that I am a newbie in PHP world. I have a table in a database and I am trying to update a row's content based on its id that I get inside the page's link.
But the update never occurs. Please help. I can't see what's wrong.

Thanks.

<?php
// Connects to your Database
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("bestcv") or die(mysql_error());
$pno=$_GET['pno'];

if(isset($_COOKIE['bestupt']))

//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['bestupt'];
$pass = $_COOKIE['bestupt_pass'];
$check = mysql_query("SELECT * FROM bestupt_login WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{

//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'] || $info['auth'] != 0)
{ header("Location: login.php");
}

//otherwise they are shown the admin area
else
if($info['auth']==0)
{

//This code runs if the form has been submitted
if (isset($_POST['submit'])) {

//This makes sure they did not leave any fields blank
if (!$_POST['name'] | !$_POST['surname'] | !$_POST['position'] ) {
die('You did not complete all of the required fields');
}

// now we insert it into the database
$pos = $_POST['position'];
$board = 0;
if($pos == 'Voting Member')
{
$board = 1;
}
else
if($pos == 'Secretary')
{
$board = 2;
}
else
if($pos == 'Treasurer')
{
$board = 3;
}
else
if($pos == 'MK Vicepresident')
{
$board = 4;
}
else
if($pos == 'HR Vicepresident')
{
$board = 5;
}
else
if($pos == 'President')
{
$board = 6;
}


$add=mysql_query("UPDATE people SET name='".$_POST['name']."', surname='".$_POST['surname']."', position='".$_POST['position']."', photo='".$_POST['photo']."', board='".$board."' WHERE pno='".$pno."' ")or die(mysql_error());


?>
<h1>Member added</h1>
<a href="admin.php">Back</a>

<?
}
else
{
$usertoedit = mysql_query("SELECT * FROM people WHERE pno = '".$pno."'")or die(mysql_error());
$info = mysql_fetch_array($usertoedit);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>Name:</td><td>
<input type="text" name="name" value="<?php echo $info['name']; ?>" maxlength="60">
</td></tr>
<tr><td>Surname:</td><td>
<input type="text" name="surname" value="<?php echo $info['surname']; ?>" maxlength="60">
</td></tr>
<tr><td>Position:</td></tr>
<tr><td>
<input type="radio" name="position" value="Alumnus">Alumnus<br>
<input type="radio" name="position" value="Member">Member<br>
<input type="radio" name="position" value="Former Member">Former Member<br>
<input type="radio" name="position" value="Voting Member">Voting Member<br>
<input type="radio" name="position" value="HR Vicepresident">HR Vicepresident<br>
<input type="radio" name="position" value="MK Vicepresident">MK Vicepresident<br>
<input type="radio" name="position" value="PR Responsible">PR Responsible<br>
<input type="radio" name="position" value="President">President<br>
<input type="radio" name="position" value="Secretary">Secretary<br>
<input type="radio" name="position" value="Treasurer">Treasurer<br>
</td></tr>
<tr><td>Photo:</td><td>
<input type="text" name="photo" value="<?php echo $info['photo']; ?>" maxlength="10">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="OK"></th></tr> </table>
</form>

<?
}
}
}
}
?>

Not sure if it will work for you, but I usually add something like this before the submit button:

<input type="hidden" name="pno" value="<?php echo $pno; ?>" />

I am assuming that "pno" is the id of the content that should be updated.

Not sure if it will work for you, but I usually add something like this before the submit button:

<input type="hidden" name="pno" value="<?php echo $pno; ?>" />

I am assuming that "pno" is the id of the content that should be updated.

I know what you mean. Cause after I press submit, it basically reloads the page, but without retransmitting the argument.
Thanks very much. It works now :)

<input type="radio" name="position" value="Alumnus">Alumnus<br>
<input type="radio" name="position" value="Member">Member<br>
<input type="radio" name="position" value="Former Member">Former Member<br>
<input type="radio" name="position" value="Voting Member">Voting Member<br>
<input type="radio" name="position" value="HR Vicepresident">HR Vicepresident<br>
<input type="radio" name="position" value="MK Vicepresident">MK Vicepresident<br>
<input type="radio" name="position" value="PR Responsible">PR Responsible<br>
<input type="radio" name="position" value="President">President<br>
<input type="radio" name="position" value="Secretary">Secretary<br>
<input type="radio" name="position" value="Treasurer">Treasurer<br>

hi, i think you have problem with primary key after postback it wont get primary key by which it will make updation , so use

<input type="hidden" name="pno" value="<?php echo $pno; ?>" />

and also you have difficult programming structure in above quote.

<input type="radio" name="position" value="1">Voting  Member<br>

At value property of each radio button insert its corresponding value like you have checked for 0,1,2,3,4,5,6.
Which will ease your task.

$add=mysql_query("UPDATE people SET name='".$_POST['name']."', surname='".$_POST['surname']."', position='".$_POST['position']."', photo='".$_POST['photo']."', board='".$board."' WHERE pno='".$_POST['pno']."' ")or die(mysql_error());

hope you understand my point
Thanks & Regards
Dilip Kumar Vishwakarma

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.