Log in

View Full Version : Update MYSQL Rows



X96 Web Design
04-14-2009, 02:18 AM
Hi All,

I want to have a simple two-field form update a table row of two values.

So far it'll read the table, but I can't get it to update the table. I'm pretty new to PHP, but can read it better than I can code it...

I have two pages, the form page (index.php) and the output (page.php), index.php holds the form, and posts the form data to page.php.

page.php holds the following: (I removed the username/password from this post)


<?php
$con = mysql_connect("localhost","My Username","My Password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("learningphp_cms", $con);
?>

<?php
$result = mysql_query("SELECT * FROM page1");
$chickenpie = mysql_query("SELECT title FROM page1");
$beefstew = mysql_query("SELECT bodytext FROM page1");

while($row = mysql_fetch_array($result))
{
echo "<h1>" . $row['title'] . "</h1> <p>" . $row['bodytext'] . "</p>";
echo "<br />";
}
$sql = 'UPDATE `my database`.`page1` SET `title` = $_POST[\'title\'], `bodytext` = $_POST[\'bodytext\'] WHERE CONVERT(`page1`.`title` USING utf8) = mysql_fetch_array($chickenpie) AND CONVERT(`page1`.`bodytext` USING utf8) = mysql_fetch_array($beefstew) LIMIT 1;';
?>


And index.php holds this: (I removed the styles)


<form action="page.php" method="post">
<h1>Website Title</h1>
<input type="text" name="title" /><br/><br/>
<h1>Site Content (Body Text)</h1>
<textarea name="bodytext"></textarea><br/>
<input type="submit" value="Update Page" name="submit" style="font-size:25px; padding:0 10px 0 10px;" />
</form>


If anyone could help, it would be greatly appreciated!

Thanks,
<X96 Web Design>

Nile
04-14-2009, 02:31 AM
Try:


<?php
$con = mysql_connect("localhost","My Username","My Password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("learningphp_cms", $con);
?>

<?php
$result = mysql_query("SELECT * FROM page1");
$chickenpie = mysql_fetch_array(mysql_query("SELECT title FROM page1"));
$beefstew = mysql_fetch_array(mysql_query("SELECT bodytext FROM page1"));

while($row = mysql_fetch_array($result))
{
echo "<h1>" . $row['title'] . "</h1> <p>" . $row['bodytext'] . "</p>";
echo "<br />";
}
//$sql = 'UPDATE `my database`.`page1` SET `title` = $_POST[\'title\'], `bodytext` = $_POST[\'bodytext\'] WHERE CONVERT(`page1`.`title` USING utf8) = mysql_fetch_array($chickenpie) AND CONVERT(`page1`.`bodytext` USING utf8) = mysql_fetch_array($beefstew) LIMIT 1;';

mysql_query("UPDATE `my datanase.`page1` SET `title` = '{$_POST['title']}', `bodytext` = '{$_POST['bodytext']}' WHERE CONVERT(`page`.title USING utf8) = '{$chickenpie}' AND CONVERT(`page1`.`bodytech` USING utf8) = '{$beefstew}' LIMIT 1") or die(mysql_error());
?>

X96 Web Design
04-14-2009, 02:49 AM
I got this:


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' SET `title` = '', `bodytext` = '' WHERE CONVERT(`page`.title USING utf8) = 'Res' at line 1

Could it be the free hosting I'm using? I really wish I had an old computer kicking around I could use for a server...

Nile
04-14-2009, 04:57 AM
Try:


mysql_query("UPDATE `my datanase.`page1` SET `title` = '".$_POST["title"]."', `bodytext` = '".$_POST["bodytext"]."' WHERE CONVERT(`page`.title USING utf8) = '".$chickenpie."' AND CONVERT(`page1`.`bodytech` USING utf8) = '".$beefstew."' LIMIT 1") or die(mysql_error());
?>