2013-08-19 62 views
0

首先,我想對我即將粘貼的代碼數量進行確認,但我不想再詳細介紹它的一些情況,這就是我給出的錯誤使用表格編輯數據庫中的記錄

我有一個名爲聯繫人的表,並希望通過表單更新表。

我不知道如果它的代碼刪除用戶不能正常使用其形式或

我剛開始學這個(前幾天),這樣的代碼可能是雜亂或不100%安全,因爲它應該是一個離線數據庫,所以我會改善它,因爲我學習。

<?php include("header.php"); 

//include database connection 
include 'db_connect.php'; 

$action = isset($_POST['action']) ? $_POST['action'] : ""; 
if($action == "update"){ 
//write query 
$query = "update contacts 
set 
name = '".$mysqli->real_escape_string($_POST['name'])."', 
surname = '".$mysqli->real_escape_string($_POST['surname'])."', 
email = '".$mysqli->real_escape_string($_POST['email'])."', 
pcode = '".$mysqli->real_escape_string($_POST['pcode'])."', 
website = '".$mysqli->real_escape_string($_POST['website'])."', 
gender = '".$mysqli->real_escape_string($_POST['gender'])."' 
mobile = '".$mysqli->real_escape_string($_POST['mobile'])."' 
phone = '".$mysqli->real_escape_string($_POST['phone'])."' 
county = '".$mysqli->real_escape_string($_POST['county'])."' 
town = '".$mysqli->real_escape_string($_POST['town'])."' 
address = '".$mysqli->real_escape_string($_POST['address'])."' 
    notes = '".$mysqli->real_escape_string($_POST['notes'])."' 
business = '".$mysqli->real_escape_string($_POST['business'])."' 
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'"; 

if($mysqli->query($query)) { 
echo "User was updated."; 
}else{ 
echo "Database Error: Unable to update record."; 
} 
} 
if($action=='delete'){ //if the user clicked ok, run our delete query 

$query = "DELETE FROM users WHERE id = ".$mysqli->real_escape_string($_GET['id']).""; 
if($mysqli->query($query)){ 
echo "User was deleted."; 
}else{ 
echo "Database Error: Unable to delete record."; 
}} 

$query = "select id, name, pcode, website, email, surname, mobile, phone, business, gender, address, town, county, notes 
from contacts 
where id='".$mysqli->real_escape_string($_REQUEST['id'])."' 
limit 0,1"; 

$result = $mysqli->query($query); 
$row = $result->fetch_assoc(); 

$id = $row['id']; 
$name = $row['name']; 
$surname = $row['surname']; 
$pcode = $row['pcode']; 
$email = $row['email']; 
$business = $row['business']; 
$phone = $row['phone']; 
$mobile = $row['mobile']; 
$gender = $row['gender']; 
$address = $row['address']; 
$county = $row['county']; 
$notes = $row['notes']; 
$town = $row['town']; 
$website = $row['website']; ?> 
<?php echo "<a href='#' onclick='delete_user({$id});'>Delete</a>"; 

?> 

<body> 
<div class="div-middle-big"> 
<!--we have our html form here where new user information will be entered--> 

<a href='index.php'>Back to index</a> 
</td> 
</tr> 
</table> 
</form> 
<div id="loader_cont"><img src="img/loaders/page_loader.gif"></div> 
<?php include'topnav.php' ?> 
<div class="container"> 
<div class="main_content row-fluid"> 
<div class="span3"> 
    <?php include'menu.php' ?> 
    <!--/.well --> 
</div> 
<!--/span--> 
<div class="span9"> 
<div class="row-fluid"> 
<div class="span12"> 
<ul class="breadcrumb br_styled no_space"> 
    <li> <a href="index.html">Dashboard</a> <span class="divider">/</span> </li> 
    <li class="active">Profile</li> 
</ul> 
<div class="widget profile_cont"> 
<header> 
    <h3>Profile: <span class="profile_title"><?php echo$name; ?> <?php echo$surname; ?></span></h3> 
    <ul class="toggle_content"> 
    <li class="arrow"><a href="#">Toggle Content</a></li> 
    </ul> 
</header> 
<section class="group"> 
<div class="info"> <img src="http://api.thumbalizr.com/?url=http://<?php echo$website; ?>&width=250" alt="Profile picture"> 
    <h4>Profile Picture</h4> 
    <div class="profile_picture"> 
    <input type="file" /> 
    <!-- <input type="submit" /> --> 
    <a href="http://<?php echo$website; ?>" class="btn">visit website</a> 
    <!-- <a href="#" class="btn">UPLOAD</a> --> 
    </div> 
    <ul> 
    <li><a href="#"><i class="sweet-user"></i> Profile</a></li> 
    <li><a href="#"><i class="sweet-settings"></i> Settings</a></li> 
    <li><a href="mailto:<?php echo$email; ?>"><i class="sweet-mail"></i> Email <?php echo$name; ?></a></li> 
    <li><a href="widgets.html"><i class="sweet-cog-4"></i> Widgets</a></li> 
    <li><a href="login.html"><i class="sweet-exit"></i> Logout</a></li> 
    </ul> 
    <div class="span3"> 
    <div class="widget"> 
     <header> 
     <h3>Grid 3</h3> 
     <ul class="toggle_content" style="display: none;"> 
      <li class="arrow"><a href="#">Toggle Content</a></li> 
     </ul> 
     </header> 
     <section class="code_align"> <code>class="span3"</code> </section> 
    </div> 
    </div> 
</div> 
<div class="details"> 
<form action='#' method='post' border='0' class="well form-horizontal"> 
    <fieldset> 
    <h4 class="group"> <span>Personal details</span> </h4> 
    <div class="control-group"> 
     <div class="controls"> </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="name">First name</label> 
     <div class="controls"> 
     <input id="name" type="text" name="name" value="<?php echo$name; ?>"> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="surname">Last name</label> 
     <div class="controls"> 
     <input id="surname" type="text" name="surname" value="<?php echo$surname; ?>"> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="business">Company Name</label> 
     <div class="controls"> 
     <input id="business" type="text" name="business" value="<?php echo$business; ?>"> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="phone">Phone number</label> 
     <div class="controls"> 
     <input id="phone" type="text" name="phone" value="<?php echo$phone; ?>"> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="mobile">Mobile number</label> 
     <div class="controls"> 
     <input id="mobile" type="text" name="mobile" value="<?php echo$mobile; ?>"> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="gender">Sex</label> 
     <div class="controls"> 
     <select class="gender" style="width:210px;" tabindex="2"> 
      <option value="<?php echo$gender; ?>"><?php echo$gender; ?></option> 
      <option value="female">Female</option> 
      <option value="male">Male</option> 
     </select> 
     </div> 
    </div> 
    <h4>Contact details</h4> 
    <div class="control-group"> 
     <label class="control-label" for="email">E-mail</label> 
     <div class="controls"> 
     <input id="email" type="text" name="email" value="<?php echo$email; ?>"> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="website">Website</label> 
     <div class="controls"> 
     <input id="website" type="text" name="website" value="<?php echo$website; ?>" data-original-title="Without the http://"> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="address">Address</label> 
     <div class="controls"> 
     <textarea id="address" rows="3" name="address" ><?php echo$address; ?></textarea> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="skypeid">Town</label> 
     <div class="controls"> 
     <input id="town" type="text" name="town" value="<?php echo$town; ?>"> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="county">County</label> 
     <div class="controls"> 
     <input id="county" type="text" name="county" value="<?php echo$county; ?>"> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="pcode">Post code</label> 
     <div class="controls"> 
     <input id="pcode" type="text" name="pcode" value="<?php echo$pcode; ?>"> 
     </div> 
    </div> 
    <h4>Notes about <?php echo$name; ?> <?php echo$surname; ?></h4> 
    <p> 
     <textarea id="notes" rows="5" name="notes" ><?php echo$notes; ?></textarea> 
    </p> 
    <div class="form-actions"> 
     <!-- so that we could identify what record is to be updated --> 
     <input type='hidden' name='id' value='<?php echo $id ?>' /> 

     <!-- we will set the action to edit --> 
     <input type='hidden' name='action' value='update' /> 
     <input type='submit' value='Edit' /> 
    </div> 
    </fieldset> 
</form> 

與上面的代碼的問題是,多數民衆贊成它沒有更新我的數據庫,我收到

Database Error: Unable to update record 

UPDATE


我已經回到我的舊文件現在這個劑量工作

好吧,我去了右後衛的基本文件,我不得不....

<meta http-equiv="refresh" content="0; url=../contacts.php"> <?php 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = 'root'; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass); 
if(! $conn) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 
$sql = "DELETE FROM contacts 
     WHERE created='$_GET[id]'"; 

mysql_select_db('pcrepairs'); 
$retval = mysql_query($sql, $conn); 
if(! $retval) 
{ 
    die('Could not delete data: ' . mysql_error()); 
} 
echo "Deleted data successfully\n"; 
mysql_close($conn); 
?> 

我現在通過在運行它之前收到此錯誤

Could not delete data: Unknown column 'created' in 'where clause' 
+0

魔杖是什麼問題? – 2013-08-19 00:05:22

+0

我在問題結束時更新了問題,對不起 –

回答

1

遺忘PHP一下,把您將在命令行中發出SQL查詢,則需要使用single quotes來表示搜索字符串。

因此,這將是這樣的:

DELETE FROM users WHERE id = '100'; 

上面有,當你通過PHP建立查詢保持真實:

$query = "DELETE FROM users WHERE id='".$mysqli->real_escape_string($_GET['id'])."'"; 

如果你的代碼的失敗,你真的需要進入調試你的代碼的心態。以更小的塊進行處理,然後重新開始工作。因此,例如,您可以嘗試在控制檯中使用硬編碼的id值執行上述查詢,並確認它是否有效。

+0

我已經回去看看舊的工作腳本。我已經更新了我的問題 –

+1

正如錯誤所述,你在'contacts'表中有'created'字段嗎?你可以添加/查看基於該字段的記錄嗎? – kaizenCoder

+0

thx :)不,我把它改爲id,它的工作原理感謝您的時間:) –

1

你可以嘗試echo'ing的$查詢值庫MySQLi?獲取該sql語句並嘗試通過數據庫手動運行它。您可能還想仔細檢查您的數據類型。如果嘗試使用字符串值設置NUMBER/INT字段,則可能會出現錯誤。

+0

感謝您的回覆,但我該怎麼做? '<?php echo'$ query'?>' –

1

你忘了逗號在SQL UPDATE語句:

$query = "update contacts 
set 
name = '".$mysqli->real_escape_string($_POST['name'])."', 
surname = '".$mysqli->real_escape_string($_POST['surname'])."', 
email = '".$mysqli->real_escape_string($_POST['email'])."', 
pcode = '".$mysqli->real_escape_string($_POST['pcode'])."', 
website = '".$mysqli->real_escape_string($_POST['website'])."', 
gender = '".$mysqli->real_escape_string($_POST['gender'])."', 
mobile = '".$mysqli->real_escape_string($_POST['mobile'])."', 
phone = '".$mysqli->real_escape_string($_POST['phone'])."', 
county = '".$mysqli->real_escape_string($_POST['county'])."', 
town = '".$mysqli->real_escape_string($_POST['town'])."', 
address = '".$mysqli->real_escape_string($_POST['address'])."', 
    notes = '".$mysqli->real_escape_string($_POST['notes'])."', 
business = '".$mysqli->real_escape_string($_POST['business'])."' 
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'"; 

您還需要查看您的HTML代碼。

編輯

一個update語句中的SQL語法是:

UPDATE my_table_name SET col1='value1', col2='value2', ... WHERE conditions 

這應該刪除查詢工作:

$query = "DELETE FROM users WHERE id='".$mysqli->real_escape_string($_GET['id'])."'"; 

如果你正在使用PHP5 +我推薦你使用PDO而不是舊的sqlite函數。 您還需要在保存到數據庫之前驗證您的數據。

+0

非常感謝:)但爲什麼刪除用戶無法正常工作?我忘了逗號嗎?如果它真的很簡單,你能向我解釋這些逗號嗎? –

+1

@RickNash編輯,希望這有助於 – yafrani

+0

我已經替換了我的舊刪除查詢與您發佈的和沒有任何反應,甚至沒有顯示錯誤信息 –

2

你好像是用在你刪除query.Does users表用戶表存在嗎?如果不是,請其更改爲contacts.Please讓我知道

感謝

+0

我已將它更改爲在我身邊發生錯誤的聯繫人,您是否看到我在回覆yafrani時發佈的鏈接?該腳本爲我工作,但在這裏它似乎缺少幾行代碼,但我試圖把它放在這個代碼,但我得到錯誤時viewig –

+0

'$ query =「DELETE FROM contacts WHERE id ='」。intval $ _GET ['id'])。「'」;' 你可以試試 謝謝 –

相關問題