PHP-ODBC:更新表单
我有一个表单,并成功地将它们连接到数据库 现在我正在尝试更新数据。不幸的是,当我点击提交按钮时,什么也没有发生。我肯定我错过了什么。请帮帮我,谢谢 config.php:PHP-ODBC:更新表单,php,forms,sql-update,odbc,Php,Forms,Sql Update,Odbc,我有一个表单,并成功地将它们连接到数据库 现在我正在尝试更新数据。不幸的是,当我点击提交按钮时,什么也没有发生。我肯定我错过了什么。请帮帮我,谢谢 config.php: <?php $conn=odbc_connect("dsn", "", ""); if (!$conn) { exit("Connection Failed : " . $conn); } ?> <?php $conn=odbc_connect("dsn", "", ""); if ($conn)
<?php
$conn=odbc_connect("dsn", "", "");
if (!$conn)
{
exit("Connection Failed : " . $conn);
}
?>
<?php
$conn=odbc_connect("dsn", "", "");
if ($conn) {
echo "Connected";
}
if (!$conn) {
exit("Connection Failed : " . $conn);
}
?>
这是我的代码:
<?php
include "config.php";
ini_set('error_reporting', E_ALL);
error_reporting(-1);
$sql = odbc_exec( $conn, "SELECT
UserId,
UserName,
UserEmail
FROM DBA.tblUser
WHERE UserId='".$_GET['UserId']."'");
if(isset($_POST['submit']))
{
$UserId=$_POST["UserId"];
$UserName=$_POST["UserName"];
$UserEmail=$_POST["UserEmail"];
//UPDATE
$stmt = odbc_exec( $conn,
"UPDATE DBA.tableUsers SET
UserName = '$UserName',
UserEmail ='$UserEmail'
WHERE UserId=$UserId");
if ($stmt) {
echo "Update Success";
echo $UserName;
echo $UserEmail;
} else {
"Error : " . odbc_errormsg();
}
}
?>
表格:
<form class="form" method="post">
<tr>
<td class = "userid">User ID</td>
<td><?php echo $UserId = odbc_result($sql,'UserId'); ?></td>
</tr>
<tr>
<td class = "name">User Name<span class="required"> * </span></td>
<td><input type="text" name="UserName" value="<?php echo $UserName = odbc_result($sql,'UserName'); ?>"></td>
</tr>
<tr>
<td class = "email">Email<span class="required"> * </span></td>
<td><input type="text" name="UserEmail" value="<?php echo $UserEmail = odbc_result($sql,'UserEmail'); ?>"></td>
</tr>
<button name="submit" type="submit" value ="submit" >Update</button>
</form>
替换
$UserName=$_POST["UserName"];
与
为什么??只是因为在表单中,名称是UserId,而不是UserName
我设法用相同的概念和变量得到了答案 config.php:
<?php
$conn=odbc_connect("dsn", "", "");
if (!$conn)
{
exit("Connection Failed : " . $conn);
}
?>
<?php
$conn=odbc_connect("dsn", "", "");
if ($conn) {
echo "Connected";
}
if (!$conn) {
exit("Connection Failed : " . $conn);
}
?>
代码:
<?php
include "config.php";
$sql = odbc_exec( $conn, "SELECT
UserId,
UserName,
UserEmail
FROM DBA.tblUser
WHERE UserId='".$_GET['UserId']."'");
if(isset($_POST['submit'])) {
$UserName=$_POST["UserName"];
$UserEmail=$_POST["UserEmail"];
$stmt = odbc_exec($conn,
"UPDATE DBA.tblUser SET
UserName = '$UserName',
UserEmail ='$UserEmail'
WHERE UserId='".$_GET['UserId']."'");
if ($stmt) {
echo "Update Successfull";
echo $UserName;
echo $UserEmail;
}
else {
"Error : " . odbc_errormsg();
}
}
?>
html:
<!DOCTYPE html>
<html>
<head>
<title>Administration</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="css/userpage.css" media="screen"/>
<body>
<content>
<div class="user-form">
<div class="user-form-heading">Update User</div><br>
<form class="form" method="post">
<h><table class ="user">
<!-- .................................. Updatable ....................................... -->
<tr>
<td class = "name">User Name<span class="required"> * </span></td>
<td><input type="text" name="UserName" value="<?php echo $UserName = odbc_result($sql,'UserName'); ?>"></td>
</tr>
<tr>
<td class = "userid">User Id</td>
<td><?php echo $UserId = odbc_result($sql,'UserId'); ?></td>
</tr>
<tr>
<td class = "email">Email<span class="required"> * </span></td>
<td><input type="text" name="UserEmail" value="<?php echo $UserEmail = odbc_result($sql,'UserEmail'); ?>"></td>
</tr>
<tr>
<td>
<f><button name="submit" type="submit" value ="submit" >UPDATE</button></f>
</table>
</form>
</div>
</content>
</body>
</head>
</html>
如何连接到数据库$conn?你贴的表格是什么?如何定义$UserId变量?您定义$UserNm变量,然后在查询中将其用作$UserName我已经更新了我的帖子@hassanclose您的表单标签仍然,在更新查询之前,当提交按钮单击@hassandefine该变量$UserId时,没有发生任何事,您的意思是什么都没有发生,数据库中没有更新,浏览器中也没有显示?@到目前为止,您如何知道您已成功连接到数据库?按照此步骤设置ODBC,我已成功连接到数据库。如果$conn失败,config.php中包含的以下代码将向我显示错误。如果$连接{exitConnection失败:.$conn;}。此外,我设法从数据库中检索$UserId。唯一的问题是我似乎无法使更新工作@san@Far尝试使用一些虚拟值,不要从表单中获取它们。只需运行一个php脚本,在表中插入一些值。如果这样做有效,那么当您单击Submit时会发生什么。页面是否重新加载?检查控制台是否有任何错误或日志文件?我尝试更新现有值我更改了来自表用户的电子邮件。但什么也没发生。无页面重新加载,数据库中的值未更改。好像我的更新按钮根本不起作用@萨那