2012-01-29 6 views
1

G'DayУ вас есть ошибка в синтаксисе SQL;

У меня есть страница php, которую я хочу отредактировать, но для жизни меня не могу понять, почему это происходит с этим erro.

У вас возникла ошибка в синтаксисе SQL; проверьте руководство, соответствующее версии вашего сервера MySQL, для правильного синтаксиса для использования около '= po_postcode =' 4060 ', email =' - ', phone =' 732997688 ', fax =' 'WHERE id =' 1 '' на линии 1

ПОМОЩЬ Я в отчаянии и схожу с ума. (Подобный код работает на другой странице, но не на этом).

Может кто-нибудь ПОЖАЛУЙСТА ПОМОЧЬ.

{

<?php 
/* 
EDIT.PHP 
Allows user to edit specific entry in database 
*/ 

// creates the edit record form 
// since this form is used multiple times in this file, I have made it a function that is easily reusable 
function renderForm($id, $name, $po_street, $po_suburb, $po_state, $po_postcode, $email, $phone, $fax, $error) 
{ 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title>Edit Record</title> 
</head> 
<body> 
<?php 
// if there are any errors, display them 
if ($error != '') 
{ 
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title>Edit Record</title> 
</head> 
<body> 

<table width="347" border="0" align="center"> 
    <tr valign="baseline"> 
     <td align="right" nowrap="nowrap"><p align="center"><img src="hartwell_banner.JPG" width="624" height="134" /></p> 
    </tr> 
</table> 
<table align="center"> 
    <tr valign="baseline"> 
     <td width="290" align="right" nowrap="nowrap"><div align="left"><h2 align="left"><p align="left"><a href="contact_insert.php">Enter a New Contact</a></p></h2></div></td> 
    <td width="290" align="center" nowrap="nowrap"><div align="left"><h2 align="center"><p align="center"><a href="index.php">Return to Index</a></p> 
    </h2> 
</div></td> 
    </tr> 
    </table> 


<form action="" method="post"> 
<input type="hidden" name="id" value="<?php echo $id; ?>"/> 
<table align="center"> 
<tr valign="baseline"> 
     <td width="98" align="right" nowrap="nowrap"><div align="left">ID:</div></td>  
     <td width="329"><input type="text" name="id" value="<?php echo $id; ?>" size="40" readonly = "readonly" /> * </td> 
    </tr> 
    <tr valign="baseline"> 
     <td width="98" align="right" nowrap="nowrap"><div align="left">Name:</div></td>  
     <td width="329"><input type="text" name="name" value="<?php echo $name; ?>" size="40" /> * </td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap="nowrap" align="right"><div align="left">Postal Street </div></td> 
     <td><input type="text" name="po_street" value="<?php echo $po_street; ?>" size="40" /> * </td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap="nowrap" align="right"><div align="left">Postal Suburb</div></td> 
     <td><Input type ="text" name="po_suburb" value="<?php echo $po_suburb; ?> " size="30" maxlength="50" >*</td> 
     <tr valign="baseline"> 

    </tr> 
    <tr valign="baseline"> 
     <td nowrap="nowrap" align="right"><div align="left">State</div></td> 
     <td><Input type ="text" name="po_state" value="<?php echo $po_state; ?>" size="5" maxlength="3" /> * </td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap="nowrap" align="right"><div align="left">Postal Postcode</div></td> 
     <td><Input type ="text" name="po_postcode" value="<?php echo $po_postcode; ?>" size="5" maxlength="4"/> * </td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap="nowrap" align="right"><div align="left">Email:</div></td> 
     <td><input type="text" name="email" value="<?php echo $email; ?>" size="40" /> * </td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap="nowrap" align="right"><div align="left">Phone:</div></td> 
     <td><input name="phone" type="text" value="<?php echo $phone; ?>" size="12" maxlength="10" /> * </td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap="nowrap" align="right"><div align="left">Fax:</div></td> 
     <td><input name="fax" type="text" value="<?php echo $fax; ?>" size="12" maxlength="10" /></td> 
    </tr> 
    <tr valign="baseline"> 
     <td nowrap="nowrap" align="right">&nbsp;</td> 
     <td> <input type="submit" name="submit" value="Submit"> * Denotes Required Field<?php 
// if there are any errors, display them 
if ($error != '') 
{ 
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> </td> 
    </tr> 

    <tr valign="baseline"> 
     <td colspan="2" align="right" nowrap="nowrap"><div align="center"><img src="hartwell_costs.JPG" alt="" width="340" height="147" /></div></td> 
    </tr> 
    </table> 
</form> 
    </body> 
</html> 
<?php 
} 



// check if the form has been submitted. If it has, process the form and save it to the database 
if (isset($_POST['submit'])) 
{ 
// confirm that the 'id' value is a valid integer before getting the form data 
if (isset($_POST['id'])) 
{ 
// get form data, making sure it is valid 

$id = mysql_real_escape_string(htmlspecialchars($_POST['id'])); 
$name = mysql_real_escape_string(htmlspecialchars($_POST['name'])); 
$po_street = mysql_real_escape_string(htmlspecialchars($_POST['po_street'])); 
$po_suburb = mysql_real_escape_string(htmlspecialchars($_POST['po_suburb'])); 
$po_state = mysql_real_escape_string(htmlspecialchars($_POST['po_state'])); 
$po_postcode = mysql_real_escape_string(htmlspecialchars($_POST['po_postcode'])); 
$email = mysql_real_escape_string(htmlspecialchars($_POST['email'])); 
$phone = mysql_real_escape_string(htmlspecialchars($_POST['phone'])); 



// check that firstname/lastname fields are both filled in 
if ($id == '' || $name == '' || $po_street == '' || $po_suburb == ''|| $po_state == '' || $po_postcode == ''|| $email == '' || $phone == '') 
{ 
// generate error message 
$error = 'ERROR: Please fill in all required fields!'; 

//error, display form 
    renderForm($id, $name, $po_street, $po_suburb, $po_state, $po_postcode, $email, $phone, $fax, $error); 
} 
else 
{ 
// save the data to the database 
    mysql_select_db($database_hartwell, $hartwell); 
mysql_query("UPDATE contact SET id= '$id', name='$name', po_street ='$po_street', po_suburb = '$po_suburb', po_state = '$po_state', = po_postcode = '$po_postcode', email ='$email', phone = '$phone', fax = '$fax' WHERE id='$id'") 
or die(mysql_error()); 

// once saved, redirect back to the view page 
header("Location: view.php"); 
} 
} 
else 
{ 
//if the 'id' isn't valid, display an error 
echo 'ID Not Valid!'; 
} 
} 
else 
// if the form hasn't been submitted, get the data from the db and display the form 
{ 

// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0) 
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) 
{ 
// query db 
$id = $_GET['id']; 
    mysql_select_db($database_hartwell, $hartwell); 
$result = mysql_query("SELECT * FROM contact WHERE id=$id") 
or die(mysql_error()); 
$row = mysql_fetch_array($result); 

// check that the 'id' matches up with a row in the databse 
if($row) 
{ 

// get data from db 
$id = $row['id']; 
$name = $row['name']; 
$po_street = $row['po_street']; 
$po_suburb = $row['po_suburb']; 
$po_state = $row['po_state']; 
$po_postcode = $row['po_postcode']; 
$email = $row['email']; 
$phone = $row['phone']; 
$fax = $row['fax']; 


// show form 
renderForm($id, $name, $po_street, $po_suburb, $po_state, $po_postcode, $email, $phone, $fax,''); 
} 
else 
// if no match, display result 
{ 
echo "No results!"; 
} 
} 
else 
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error 
{ 
echo 'No ID Value!'; 
} 
} 
?> 
+1

У вас есть знак без пробелов: '= po_postcode ='. И 'set id = '$ id'' и' где id =' $ id'' в одном UPDATE немного странно. И если 'id' - это число, то не указывайте его значение, это плохая привычка, с которой MySQL позволяет вам уйти, но все, что он делает, - это сделать работу сервера более сложной. –

ответ

1

удалить знак равенства здесь:

'$po_state', = po_postcode 

mysql_query("UPDATE contact SET id= '$id', name='$name', po_street ='$po_street', po_suburb = '$po_suburb', po_state = '$po_state', po_postcode = '$po_postcode', email ='$email', phone = '$phone', fax = '$fax' WHERE id='$id'") 
+0

Спасибо. Невозможно увидеть небоскреб для причалов. Опечатка – Technophobe

4

Ошибка прямо в запросе, так же, как сообщение об ошибке говорит:

, = po_postcode = '$po_postcode', 
^
    | 
    + this doesn't belong here 
1

Так проблема здесь = po_postcode = '$po_postcode',

Смежные вопросы