The following script worked when I had hard coded variables. When I added a dropdown to select a variable from the database, the problem began.
When I execute 100.php, the dropdown populates correctly, but I get these errors:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/kenny22/public_html/Models/models100.php on line 60
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/kenny22/public_html/Models/models100.php on line 68
When I select a variable and submit, I get correct output and no errors.
When I edit 1 row of the output and submit again, I get the errors and the update query does not work.
<select name="chooser">
<?
$HOST = 'localhost';
$DATABASE = '*';
$USER = '*';
$PASSWORD = '*';
if(!$conn=mysql_connect('localhost','x','x')) {
echo("
<li>
Can't connect to $HOST as $USER");
echo("<li>
MySQL Error: ".mysql_error());
die;
}
if (!mysql_select_db($DATABASE,$conn)) {
echo("<li>
We were unable to select database $DATABASE");
die;
}
?>
<?
$sql = "SELECT distinct Domain FROM ModelFaces order by Domain";
if ($result = mysql_query($sql)) {
if (mysql_num_rows($result)) {
while ($row = mysql_fetch_assoc($result)) {
echo "<option value=\"{$row['Domain']}\">{$row['Domain']}</option>";
}
}
}
mysql_close();
?>
</select>
<select name="rate">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<input type="submit" value="Go!">
</form>
<?php
$chooser = $_POST['chooser'];
$ratelo = $_POST['rate'];
$ratehi = $ratelo+.99;
$host="localhost"; // Host name
$username="x"; // Mysql username
$password="x"; // Mysql password
$db_name="kenny22_Models"; // Database name
$tbl_name="ModelFaces"; // Table name
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql3="SELECT ID, Rating, Image, Link, Domain FROM $tbl_name where Domain='$chooser' and Rating between $ratelo and $ratehi order by Rating DESC, Image";
$sql4 = "SELECT COUNT(*) FROM $tbl_name WHERE Domain='$chooser' AND Rating BETWEEN $ratelo AND $ratehi";
$result3=mysql_query($sql3);
$result4 = mysql_query($sql4);
$countrows = mysql_fetch_row($result4);
?>
<table width="640" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="640" border="0" cellspacing="1" cellpadding="0">
<?php
while($rows3=mysql_fetch_array($result3)){
?>
<tr>
<td align="left"><? $id[]=$rows3['ID']; ?><? echo $rows3['ID']; ?></td>
<td align="left"><? echo $rows3['Link']; ?></td>
<td align="left"><? echo $rows3['Image']; ?></td>
<td align="left"><input name="Rating[]" type="text" id="Rating" value="<? echo $rows3['Rating']; ?>"></td>
<td align="left"><img src="<? echo $rows3["Image"]; ?>" height=106 width=80></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center">
<input type="submit" name="Submit" value="Submit">
</td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
if($Submit){
for($i=0;$i<$count;$i++){
$sql6="UPDATE $tbl_name SET Rating=$Rating[$i] WHERE ID=$id[$i]";
$result6=mysql_query($sql6);
}
}
mysql_close();
?>