2012-04-23 122 views
0

很簡單,我的MySQL數據庫在提交表單時沒有更新。整個文件的代碼可以看到下面,幸虧:請注意,checkuserlog.php文件不包括連接到MySQL數據庫中的文件...MySQL數據庫不能用PHP更新

<?php 
include_once("../scripts/checkuserlog.php"); 
?> 
<?php 
if (!isset($_SESSION['id']) || !isset($_SESSION['username'])){ 
    echo 'Please <a href="../login.php">log in</a> to view'; 
    exit(); 
} 

// set variables 
$id = $logOptions_id; 
$school = ""; 
$form_teacher = ""; 
$room = ""; 
$success_msg = ""; 
$error_msg = ""; 
$errorMsg = ""; 
$sch_website = ""; 
$sch_phone = ""; 
$sch_email = ""; 
$notes = ""; 
// end variables and begin parsing 
if (isset($_POST['parse_var'])){ 

if ($_POST['parse_var'] == "allstuff"){ 
    $form_teacher = strip_tags($_POST['form_teacher']); 
    $form_teacher = str_replace("'", "&#39;", $form_teacher); 
    $form_teacher = str_replace("`", "&#39;", $form_teacher); 
    $form_teacher = mysql_real_escape_string($form_teacher); 
    $school = strip_tags($_POST['school']); 
    $school = str_replace("'", "&#39;", $school); 
    $school = str_replace("`", "&#39;", $school); 
    $school = mysql_real_escape_string($school); 
    $room = strip_tags($_POST['room']); 
    $room = str_replace("'", "&#39;", $room); 
    $room = str_replace("`", "&#39;", $room); 
    $room = mysql_real_escape_string($room); 
    $sch_website = strip_tags($_POST['sch_website']); 
    $sch_website = str_replace("'", "&#39;", $sch_website); 
    $sch_website = str_replace("`", "&#39;", $sch_website); 
    $sch_website = mysql_real_escape_string($sch_website); 
    $sch_phone = strip_tags($_POST['sch_phone']); 
    $sch_phone = str_replace("'", "&#39;", $sch_phone); 
    $sch_phone = str_replace("`", "&#39;", $sch_phone); 
    $sch_phone = mysql_real_escape_string($sch_phone); 
    $sch_email = strip_tags($_POST['sch_email']); 
    $sch_email = str_replace("'", "&#39;", $sch_email); 
    $sch_email = str_replace("`", "&#39;", $sch_email); 
    $sch_email = mysql_real_escape_string($sch_email); 
    $notes = strip_tags($_POST['pln_notes']); 
    $notes = str_replace("'", "&#39;", $notes); 
    $notes = str_replace("`", "&#39;", $notes); 
    $notes = mysql_real_escape_string($notes); 

    $sqlUpdate = mysql_query("UPDATE table SET form_teacher='$form_teacher', school='$school', form_room='$room', sch_website='$sch_website', sch_phone='$sch_phone', sch_email='$sch_email' pln_notes='$notes' WHERE mem_id='$id' LIMIT 1"); 
    if ($sqlUpdate){ 
     $success_msg = '<img src="images/round_success.png" width="20" height="20" alt="Success" />We successfully updated your personal information.'; 
    } else { 
     $error_msg = '<img src="images/round_error.png" width="20" height="20" alt="Failure" /> ERROR: Problems arose during the information exchange, please try again later.</font>'; 
    } 
} 
} 
// end parsing 
// get existing data 
$sql_default = mysql_query("SELECT * FROM table WHERE mem_id='$id'"); 

while($row = mysql_fetch_array($sql_default)){ 

    $school = $row["school"]; 
    $form_teacher = $row["form_teacher"]; 
    $room = $row["form_room"]; 
    $sch_website = $row["sch_website"]; 
    $sch_phone = $row["sch_phone"]; 
    $sch_email = $row["sch_email"]; 
    $notes = $row["pln_notes"]; 
    $notes = str_replace("<br />", "", $notes); 
    $notes = stripslashes($notes); 


} 
// end get data 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1"> 
<title>Edit Your Planner</title> 
<link href="../style/main.css" rel="stylesheet" type="text/css" /> 
<link rel="icon" href="../favicon.ico" type="image/x-icon" /> 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" /> 
</head> 
<body> 
<?php include_once "../header_template.php"; ?> 
<div id="maincont"> 
<h1>Edit your Planner <span style="float:right;margin-right:270px;"><a href="planner.php" class="medium awesome green">View Planner</a></span></h1> 
<table border="0" width="950"> 
<tr><td width="680" valign="top"> 
<h2><?php echo $success_msg; ?><font color="#FF0000"><?php echo $errorMsg; ?></font></h2> 
<table width="100%" border="0"> 
<tr><td width="50%" valign="top"> 
<form name="editplanner" method="post" action="planner_edit.php"> 
<p><strong>School:</strong><br /><input type="text" name="school" id="school" placeholder="What's the name of your school?" value="<?php echo $school; ?>" class="formfield" size="45"></p> 
<p><strong>Form Teacher:</strong><br /><input type="text" name="form_teacher" id="form_teacher" value="<?php echo $form_teacher; ?>" placeholder="Who is you form/class teacher?" class="formfield" size="45"></p> 
<p><strong>Form Room:</strong><br /><input type="text" name="room" id="room" class="formfield" value="<?php echo $room; ?>" placeholder="Room number or room name" size="45"></p> 
<p><strong>School Website:</strong><br /><input type="text" name="sch_website" id="sch_website" class="formfield" value="<?php echo $sch_website; ?>" placeholder="School website URL here" size="45"></p> 
<p><strong>Phone:</strong><br /><input type="text" name="sch_phone" id="sch_phone" class="formfield" value="<?php echo $sch_phone; ?>" placeholder="School phone number" size="45"></p> 
<p><strong>Email:</strong><br /><input type="text" name="sch_email" id="sch_email" class="formfield" value="<?php echo $sch_email; ?>" placeholder="School email address" size="45"></p> 
</td> 
<td width="50%" valign="top"> 
<h2>Notes</h2> 
<textarea name="pln_notes" id="pln_notes" placeholder="Add notes to your planner here..." style="width:340px;height:267px;"><?php echo $notes; ?></textarea> 
</td></tr></table> 
<input name="parse_var" type="hidden" value="allstuff" /> 
<input type="submit" name="updateBtn2" class="formfield" value="Update!" /></form> 
<hr /> 
</td> 
<td width="270" valign="top"><?php include_once "../temps/sidebar.php"; ?> 
</td></tr></table> 
</div> 
<?php include_once "../footer_template.php"; ?> 
</body> 
</html> 
+1

你做了什麼來嘗試和自己調試代碼?任何錯誤消息? – 2012-04-23 19:41:56

+2

querry中缺少逗號 – 2012-04-23 19:42:46

+0

當您運行代碼時,您的成功消息是否顯示?或者你的錯誤信息?你的PHP日誌中是否有任何警告/錯誤? – Developer 2012-04-23 19:42:46

回答

0

它看起來並不像$ logOptions_id被設定在任何地方,這樣的$ id可能是空

而且,您的更新查詢缺少「sch_email」後一個逗號:

$sqlUpdate = mysql_query("UPDATE table SET form_teacher='$form_teacher', school='$school', form_room='$room', sch_website='$sch_website', sch_phone='$sch_phone', sch_email='$sch_email', pln_notes='$notes' WHERE mem_id='$id' LIMIT 1"); 
+0

你不需要圍繞一個整數的引號......但它們也不會傷害任何東西。這不是問題。 – Eli 2012-04-23 19:44:49

2

您查詢看起來錯了,你錯過了之前pln_notes =昏迷,和更新查詢不需要限制1。

0

請看看這個,如果你的ID是在第一個SQL查詢整數/ 您的代碼:

$sqlUpdate = mysql_query("UPDATE table SET form_teacher='$form_teacher', school='$school', form_room='$room', sch_website='$sch_website', sch_phone='$sch_phone', sch_email='$sch_email' pln_notes='$notes' WHERE mem_id='$id' LIMIT 1"); 

錯誤(對我來說):

mem_id='$id' 

因爲如果mem_idInteger請使用:

mem_id=$id 

同樣的情況發生對於第二個SQL查詢:

$sql_default = mysql_query("SELECT * FROM table WHERE mem_id='$id'"); 

修正如果mem_idInteger

mem_id=$id 

希望這會解決您的錯誤。

0

是的,這裏的其他答案是正確的。您的更新聲明缺少最後一列正在更新的逗號。

但是,爲了將來的參考,您可以使用實際記錄查詢中的錯誤的實用程序函數來跟蹤SQL查詢錯誤。下面是一個例子。

function db_query($query) { 
    $result = mysql_query($query); 

    $error_text = mysql_error(); 

    if ($error_text) { 
     trigger_error('SQL ERROR: ' . $error_text, E_USER_WARNING); 
    } 

    return $result; 
} 

這應該有助於您追蹤將來SQL相關的錯誤。