Jump to content

[SOLVED] Can't update Database


XpertWorlock

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/112108-solved-cant-update-database/
Share on other sites

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

$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.

 

$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.

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;



?>

 

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

:'(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.