使用表单编辑数据库中的记录

时间:2013-08-19 00:04:04

标签: php forms mysqli edit

首先,我想对我要粘贴的代码数量进行评估,我不想再解决它的问题,而是给我一些错误

我有一个名为contacts的表,并希望通过表单更新表。

我不确定它的表单是否为删除用户的代码是否正常工作

我刚刚开始学习这个(几天前)所以代码可能很乱或者不是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

更新


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

好的,我回到了我的基本文件....

<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 个答案:

答案 0 :(得分:2)

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

由于

答案 1 :(得分:1)

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

答案 2 :(得分: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函数。 您还需要在保存到数据库之前验证数据。

答案 3 :(得分: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值执行上述查询,并确认其有效。