Good day:

I'm creating a form to write SEO into my php page head and have run into a block on the mysql_query() UPDATE function. I just cannot seem to get it to write multiple fields in a row.

What I've done so far is as follows:

<?
if ($_POST['seo'] == Save) { 
	$data = mysql_real_escape_string(trim($_POST['data'])); 
	$res = mysql_query("UPDATE seo_en SET (".title." ".description.", ".keywords.") = '".$data."' WHERE id = 1");
	if (!$res) 
		die("Error saving the record!  Mysql said: ".mysql_error()); 
	header("Location: home.php");
}
	$query = mysql_query("SELECT 'title', 'description', 'keywords' FROM content_en WHERE id = 1"); 
	$data = mysql_fetch_array($query); 
?>
<form name="seo" action="home.php" method="post">
	Title: <input size="66" type="text" name="title[]" id="title[]" value="<? echo $data['title']; ?>"><br/>
	Description: <input size="100" type="text" name="description[]" id="description[]" value="<? echo $data['description']; ?>"><br/>
	Keywords: <input size="100" type="text" name="keywords[]" id="keywords[]" value="<? echo $data['keywords']; ?>"><br>
	<input type="submit" name="seo" value="Save" />
</form>

With the code above, I can read data from the 3 fields into INPUT fields, but cannot write back on Save.
The MySQL error I receive is as follows:
Error saving the record! Mysql said: 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 '(title description, keywords) = '' WHERE id = 1' at line 1
My scripts work fine in every way, but I just cannot get teh SYNTAX down for the $res line.

Any help would be greatly appreciated.

Best Regards,
dennishall

Recommended Answers

All 2 Replies

try:

<?php
if ( isset($_POST['seo']) && $_POST['seo'] == 'Save' )
{
	foreach($_POST['title'] as $index=>$value)
	{
		$title = mysql_real_escape_string(trim($_POST['title'][$index])); 
		$description = mysql_real_escape_string(trim($_POST['description'][$index])); 
		$keywords = mysql_real_escape_string(trim($_POST['keywords'][$index])); 
		$res = mysql_query("UPDATE `content_en` SET `title`='$title',`description`='$description',`keywords`='$keywords' WHERE `id`=1");
		if (!$res) 
			die("Error saving the record!  Mysql said: ".mysql_error()); 
		header("Location: home.php");
	exit;
	}
}
$query = mysql_query("SELECT `id`, `title`, `description`, `keywords` FROM `content_en` WHERE `id` = 1") or die( mysql_error() ); 
echo '<form name="seo" action="home.php" method="post">';
while( $data = mysql_fetch_assoc($query) )
{
?>

	<div>Title: <input size="66" type="text" name="title[<?php echo $data['id']; ?>]" id="title[<?php echo $data['id']; ?>]" value="<? echo htmlentities($data['title'],ENT_QUOTES); ?>"></div>
	<div>Description: <input size="100" type="text" name="description[<?php echo $data['id']; ?>]" id="description[<?php echo $data['id']; ?>]" value="<? echo htmlentities($data['description'],ENT_QUOTES); ?>"></div>
	<div>Keywords: <input size="100" type="text" name="keywords[<?php echo $data['id']; ?>]" id="keywords[<?php echo $data['id']; ?>]" value="<? echo htmlentities($data['keywords'],ENT_QUOTES); ?>"></div>
<?php
}
?>
	<div><input type="submit" name="seo" value="Save" /></div>
</form>

Hi hielo:
You have saved my day and I thank you dearly :)
I see what I was not doing and will learn from this.

You solved my issue.

I did needed to make one small adjustment. I had to remove the exit from line 13 as it was stopping the form from refresshing upon page reload. That was the only adjustment.

Thanks ever so much for this and I hope one day I can be of assistance to you.
If there is anything, IM or email me.

All the best to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.