XpertWorlock Posted June 26, 2008 Share Posted June 26, 2008 I'm trying to update a database using 2 PHP files. imagetag.php is the main one and has echo "<td>Brown Hair : <input name=\"img_brownhair\" type=\"checkbox\" value=\"true\" $checked ></td>"; updatetag.php has <?php $con = mysql_connect("******","*******","**********"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("****_*****", $con); $img_brownhair = $_POST['img_brownhair']; $img_name = $_POST['name']; echo $img_brownhair; if ($img_brownhair == "true") {mysql_query("UPDATE image SET img_brownhair = '1' WHERE img_name = $img_name"); echo "=1"; } else {mysql_query("UPDATE image SET img_brownhair = '0' WHERE img_name = $img_name"); echo "=0"; }; ?> $img_name is right $img_brownhair is right everything works, it just won't update the database. Question in a question, my checkbox's value is true, even if I put anything else for the value it doesn't change. That's why I have this line if ($img_brownhair == "true") and even if I try to change the variable, it still print's out true. PHP 5.25 Quote Link to comment Share on other sites More sharing options...
fenway Posted June 27, 2008 Share Posted June 27, 2008 $img_name needs quotes around it. Quote Link to comment Share on other sites More sharing options...
XpertWorlock Posted June 27, 2008 Author Share Posted June 27, 2008 I think you meant it this way WHERE 'img_name' = $img_name"); also I tried single quotes around the variable, and just to be safe, single quotes around them both. where it echo's echo "=0"; or echo "=1"; works, so it's definetely not the if statement or the form itself, its something with the database Quote Link to comment Share on other sites More sharing options...
XpertWorlock Posted June 27, 2008 Author Share Posted June 27, 2008 $result = mysql_query("SELECT * FROM image WHERE 'img_name' = $img_name"); Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*******/public_html/updatetag.php on line 30 when called upon, 'img_name' is exactly the same as $img_name. So I'm guessing this has something to do with the database not updating. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 have you checked if after doin some mysql_query(), does it really return anything? possibly it didnt returned anything or that there was an error. Quote Link to comment Share on other sites More sharing options...
fenway Posted June 27, 2008 Share Posted June 27, 2008 $result = mysql_query("SELECT * FROM image WHERE 'img_name' = $img_name"); Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*******/public_html/updatetag.php on line 30 when called upon, 'img_name' is exactly the same as $img_name. So I'm guessing this has something to do with the database not updating. No, you wan't the opposite: $result = mysql_query("SELECT * FROM image WHERE img_name = '$img_name'"); And yes, check mysql_error() too. Quote Link to comment Share on other sites More sharing options...
XpertWorlock Posted June 27, 2008 Author Share Posted June 27, 2008 have you checked if after doin some mysql_query(), does it really return anything? If you mean querying by : $result = mysql_query("SELECT * FROM image"); while($row = mysql_fetch_array($result)) { echo $row['img_name'] . ".... " . $row['img_brownhair']; echo "<br />"; } This displays the Image name ...... 0 or 1 Although something happens if I do this $result = mysql_query("SELECT * FROM image where img_name = '$img_name'"); while($row = mysql_fetch_array($result)) { echo $row['img_name'] . ".... " . $row['img_brownhair']; echo "<br />"; } Nothing returns at all When it should give me only the row of the exact image, and it's img_brownhair value. I changed it to WHERE img_name = '$img_name'"); It makes no difference. Thanks, I'm still testing different things out, to see if I can find out what it's problem is. ::::FULL CODE:::: <?php $con = mysql_connect("******","****","*****"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("*******", $con); $img_brownhair = $_POST['img_brownhair']; $img_name = $_POST['name']; echo $img_brownhair; echo "<br><br>"; if ($img_brownhair == "true") {mysql_query("UPDATE image SET img_brownhair = '1' WHERE img_name = '$img_name'"); echo "img_brownhair should be 1"; } else {mysql_query("UPDATE image SET img_brownhair = '0' WHERE img_name = '$img_name'"); echo "img_brownhair should be 0"; }; echo "<br><br>"; $result = mysql_query("SELECT * FROM image where img_name = '$img_name'"); while($row = mysql_fetch_array($result)) { echo $row['img_name'] . ".... " . $row['img_brownhair']; echo "<br />"; } echo "<br><br>"; echo $img_name; ?> Quote Link to comment Share on other sites More sharing options...
XpertWorlock Posted June 27, 2008 Author Share Posted June 27, 2008 I fixed it, when it was loading the name onto the first page, it was putting a space in front of every img_name, so when I loaded it on the second page example update ' fake.jpg = $fake.jpg' there was nothing there to update because of the space. web design.... Live and learn, sometimes the very hard way :'( Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.