I am trying to get users to enter their name when submitting a value which should then be stored in a particular position depending on what the doc_id is, but it changes all the names in the table to the name which has just been submitted.
i think its because foreach is supposed to be used on arrays and the 'name' variable is created outside of an array.
Here is the coding.
<html>
<head>
<title>Dropdown Menu</title>
<head>
<body>
<?php
if(!isset($_POST['submit'])){
?>
<?php
$host = "localhost";
$user = "root";
$pass = "mysql";
$db = "test";
$connection = mysql_connect($host,$user,$pass) or die ("ERROR:Unable to connect".mysql_error());
mysql_select_db($db) or die ("ERROR: Unable to connect to DB".mysql_error());
$query = "SELECT * FROM menu";
$result = mysql_query($query) or die ("ERROR:Unable to run query".mysql_error());
echo "<form action=$_SERVER[PHP_SELF] method=POST name=rating>";
echo "<table border=0 cellspacing=3 cellpadding=3>";
echo "<font face=Verdana size=-1>Please Enter In Your Ldap name</font>";
echo "<tr>";
echo "<td><font face=Verdana size=-1>Name: </td> <td><input type=text name=name>";
echo "</table>";
echo "<br>";
echo "<table border=1 cellpadding=3 cellspacing=3>\n";
echo "<tr>";
echo "<td><p align=center><font face=Verdana size=-1><b>ID</center></td>";
echo "<td><p align=center><font face=Verdana size=-1><b>Document Title</td>";
echo "<td><p align=center><font face=Verdana size=-1><b>Rating</b></td>";
echo "<td><p align=center><font face=Verdana size=-1><b>Status</b></td>";
echo "</tr>\n";
while ($row=mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td><font face=Verna size=-1>".$row['doc_id']."</td>";
echo "<td><font face=Verdana size=-1>".$row['doc']."</td>";
if ($row['rating'] < 10 ){
echo "<td><select name=".$row['doc_id']."><option value= ''> </option>\n
<option value=1>1 - Poor</option>\n
<option value=2>2 - Average</option>\n
<option value=3>3 - Excellent</option>\n
</select>\n";
echo "</td>";
echo "<td><p align=center><img src=bluedot.gif name=need_rating height=10px width=10px></td>";
} else {
echo "<td><p align=center><font face=Verdana size=-1>".$row['rating']."</center></font></td>";
echo "<td><p align=center><img src=reddot.gif name=already_rated height=10px width=10px></td>";
}
echo "</tr>\n";
}
echo "</table>\n";
echo "<table border=0 cellpadding=3 cellspacing=3>";
echo "<tr>";
echo "<td><font face=Verdana size=-1>Rating Required</td> <td><img src=bluedot.gif height=10px width=10px></td></tr>";
echo "<tr><td><p align=right><font face=Verdana size=-1>Rated</td> <td><img src=reddot.gif height=10px width=10px></td>";
echo "</tr>";
echo "</table>";
echo "<br>";
echo "<input type=submit name=submit value=submit>";
echo "</form>";
#}
echo "<a href=http://localhost/brainstorm/phpFreaks/entryform.php><font face=Verdana size=-1>Back To Form</a>";
?>
<?php
} else {
$host = "localhost";
$user = "root";
$pass = "mysql";
$db = "test";
$connection = mysql_connect($host,$user,$pass) or die ("ERROR:Unable to connect".mysql_error());
mysql_select_db($db) or die ("ERROR: Unable to connect to DB".mysql_error());
$select = "SELECT * FROM menu";
$select_result = mysql_query($select) or die ("ERROR:Unable To Run $select".mysql_error());
$name = empty($_POST['name']) ? die ("ERROR:Please log in with your Ldap".mysql_error()): mysql_escape_string($_POST['name']);
/*
$rater = "UPDATE menu SET rater=$_POST[name] WHERE doc_id = $_POST[doc_id]";
$update_rater = mysql_query($rater) or die ("ERROR:Unable to record username".mysql_error());
*/
echo "<font face=Verdana size=-1>Your Rating(s) have been recorded</font>";
echo "<br>";
echo "<br>";
echo "<font face=Verdana size=-1><a href=http://localhost/brainstorm/phpfreaks/form4.php>View Table</a>";
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not integer
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
foreach ($_POST as $doc_id => $rating){
if ($doc_id == 'submit') {
exit();
} //exits when it gets to submit
$update_table =sprintf("UPDATE menu SET rating = rating + %s, num_of_votes = num_of_votes + 1, rater = %s WHERE doc_id = %s",
quote_smart($rating),
quote_smart($name),
quote_smart($doc_id));
$result = mysql_query($update_table) or die("ERROR:Unable To Run $update_table".mysql_error());
}
}
does anyone no why it is doing this?