Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This has been bugging me for 3 days now.. I'm new to this and trying to get my head round something. I have a form which involves 3 fields. Firstname, Surname, Marks. I have used a while loop to generate the table from a mysql table. I have used a text box and used the loop to call the text box after the 'ID' so each text box is named uniquely. I am then using a post method to send values to a second page which will update the 'marks' column with the value the user has just put in.. this is where I am finding my problem!

This is the initial page.

<html> 
<head><title>Please Enter Your Surname</title></head> 
<body> 
<center> 
<h2><font color=blue>Please Enter Your Surname</font></h2><p> 

<form action="insert.php" method="POST"> 

<?php

$db = mysql_connect("localhost","root","");
if (!$db)
{
    do_error("Could not connect to the server");
}

mysql_select_db("session6",$db)or do_error("Could not connect to the database");


$result = mysql_query("SELECT * FROM members ORDER BY id",$db);


$rows=mysql_num_rows($result);


if(!$rows)
{
    do_error("No results found");
}
else
{
    echo "<table border=3 cellspacing=1 cellpadding=1
    align=center bgcolor=lightblue>\n";
    echo "<caption><h2><font color=blue> Members Details    
            </font></h2></caption>\n";
    echo "<tr><th>Member Id</th><th>Firstname</th><th>Mark</th></tr>\n";
    while ($row = mysql_fetch_array($result))
    {

        echo "<tr>";
        echo "<td strong>" . $row['Id'] . "</td>";
        echo "<td strong>" . $row['Firstname'] . "</td>";?>
        <td strong><input type="text" name="<?php echo $row['Id']; ?>" size="20"></td>
        <tr>
        <?php



    }
    ?><input type="hidden" name="no_of_rows" value="<?php echo $rows; ?>">
    <?php
    echo "</table>\n";
}

mysql_close($db) or do_error("Could not close connection");

function  do_error($error)
{
    echo  $error;
    die();
}

?>
<input type="submit" value="Search"> 
<input type="reset" value="Reset"> 

</form> 

</body></html>
`

Then the update is done here which is where I seem to be having a problem:

<html>
<body>
<?php
$db = mysql_connect("localhost","root","");
if (!$db)
{
    do_error("Could not connect to the server");
}

mysql_select_db("marks",$db)or do_error("Could not connect to the database");

$i=1;
while ($i <= $_POST["no_of_rows"])// or $_POST["No_of_Rows"] from form
{   

    $insertsql = "UPDATE members SET mark = " . $_POST[$i] . " WHERE Id = " . $row['Id'] . ";"; 
    echo $_POST['$i'];
    $i++;

}

?>

</body></html>

When I echo $_POST[$i'] it shows the correct values but does not update the DB, and I'm not about ready to throw my laptop in the bin! ha! I know it is prob going to be something stupid I just can't see what, so any help would be appreciated.

share|improve this question
    
Drop 'mysql_*' and start using PDO because it's deprecated... –  Yassine Houssni Nov 6 '13 at 13:04

3 Answers 3

up vote 1 down vote accepted

You're missing the single quotes in your update query. This would help:

$insertsql = "UPDATE `members` SET `mark` = '" . $_POST[$i] . "' WHERE `Id` = '" . $row['Id'] . "' ;"; 

you are also not running the mysql_query query command for the update

lastly you are using the mysql php commands which are deprecated. Use mysqli or pdo instead. and don't forget to escape data in your queries to prevent sql injections

share|improve this answer

Problem is the single quotes here, forcing to literal '$i' which probably isnt a key in $_POST

echo $_POST["$i"];
share|improve this answer

No need to use quotes when variable is used: $_POST[$id];

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.