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

更新


我已经回到了我的旧文件,现在这个dosent工作

好的,我马上回到我拥有的基本文件....

<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'
4

4 回答 4

2

您似乎在删除查询中使用了用户表。用户表是否存在?如果不存在,请将其更改为联系人。请告诉我

谢谢

于 2013-08-19T01:15:34.620 回答
1

如果您要发出 SQL 查询,比如在命令行中,暂时忘记 PHP,您需要使用single quotes来表示搜索字符串。

所以它会这样:

DELETE FROM users WHERE id = '100';

当您通过 PHP 构造查询时,上述内容必须保持正确:

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

如果你的代码失败了,你真的需要进入调试代码的心态。以较小的块接近它并按自己的方式工作。因此,例如,您可以尝试在控制台中使用硬编码的 id 值执行上述查询并确认它有效。

于 2013-08-19T01:16:14.847 回答
1

您可以在通过 mysqli 运行之前尝试回显 $query 值吗?获取该 sql 语句并尝试通过数据库手动运行它。您可能还想仔细检查您的数据类型。例如,如果您尝试使用字符串值设置 NUMBER/INT 字段,则可能会出现错误。

于 2013-08-19T00:09:11.770 回答
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 代码。

编辑

更新语句的 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 函数。您还需要在保存到数据库之前验证您的数据。

于 2013-08-19T00:12:37.960 回答