PHP SQL表单插入
我开发了一个表单,可以在数据库中插入许多内容。但不知何故,当页面被填满时,它只插入数据库管理员的用户密码。我如何解决这个问题PHP SQL表单插入,php,sql,Php,Sql,我开发了一个表单,可以在数据库中插入许多内容。但不知何故,当页面被填满时,它只插入数据库管理员的用户密码。我如何解决这个问题 <?php //include the connection file require_once('connection.php'); require_once("validation.php"); if( isset($_POST['send']) && (!validateName($_POST['name']) || !validateEm
<?php
//include the connection file
require_once('connection.php');
require_once("validation.php");
if( isset($_POST['send']) && (!validateName($_POST['name']) || !validateEmail($_POST['email']) || !validatePasswords($_POST['pass1'], $_POST['pass2']) || !validateContact($_POST['contact']) || !validateAge($_POST['age'])) ):?>
<div id="error">
<ul>
<?php if(!validateName($_POST['name'])):?>
<li><strong>Invalid Name:</strong> We want names with more than 3 letters.</li>
<?php endif?>
<?php if(!validateEmail($_POST['email'])):?>
<li><strong>Invalid E-mail:</strong> Type a valid e-mail please.</li>
<?php endif?>
<?php if(!validatePasswords($_POST['pass1'], $_POST['pass2'])):?>
<li><strong>Passwords are invalid:</strong> Passwords doesnt match or are invalid!</li>
<?php endif?>
<?php if(!validateContact($_POST['contact'])):?>
<li><strong>Please enter your contact number.</strong></li>
<?php endif?>
<?php if(!validateAge($_POST['age'])):?>
<li><strong>Please enter your age</strong></li>
<?php endif?>
</ul>
</div>
<?php elseif(isset($_POST['send'])):?>
<div id="error" class="valid">
<ul>
<?php $query = "INSERT INTO employee (name, password, email, contact, age, gender, location, skill) ";
$query .= "('$name', '$password', '$email','$contact','$age','$gender','$location','$skill')";
// run the query
mysql_query($query);?>
<li><strong>Congratulations!</strong> All fields are OK ;)</li>
</ul>
</div>
<?php endif?>
- 无效名称:我们希望名称包含3个以上的字母
- 无效电子邮件:请键入有效电子邮件
- 密码无效:密码不匹配或无效李>
- 请输入您的联系电话。
- 请输入您的年龄
我已经为您编写了完整的代码:
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Name:</td>
<td><input type="text" name="name" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Email:</td>
<td><input type="text" name="email" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Pass1:</td>
<td><input type="text" name="pass1" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Contact:</td>
<td><input type="text" name="contact" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Age:</td>
<td><input type="text" name="age" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" /></td>
</tr>
</table>
<input type="hidden" name="id" value="" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>
<?php require_once('../../../Connections/yourconnection.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO employee (id, name, email, pass1, contact, age) VALUES (%s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['pass1'], "text"),
GetSQLValueString($_POST['contact'], "text"),
GetSQLValueString($_POST['age'], "text"));
mysql_select_db($database_yourdatabase, $databasename);
$Result1 = mysql_query($insertSQL, $databasename) or die(mysql_error());
$insertGoTo = "sucess.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_yourdatabase, $databasename);
$query_query = "SELECT * FROM employee";
$query = mysql_query($query_query, $databasename) or die(mysql_error());
$row_query = mysql_fetch_assoc($query);
$totalRows_query = mysql_num_rows($query);
mysql_free_result($query);
?>
我已经为您编写了完整的代码:
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Name:</td>
<td><input type="text" name="name" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Email:</td>
<td><input type="text" name="email" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Pass1:</td>
<td><input type="text" name="pass1" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Contact:</td>
<td><input type="text" name="contact" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Age:</td>
<td><input type="text" name="age" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" /></td>
</tr>
</table>
<input type="hidden" name="id" value="" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>
<?php require_once('../../../Connections/yourconnection.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO employee (id, name, email, pass1, contact, age) VALUES (%s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['pass1'], "text"),
GetSQLValueString($_POST['contact'], "text"),
GetSQLValueString($_POST['age'], "text"));
mysql_select_db($database_yourdatabase, $databasename);
$Result1 = mysql_query($insertSQL, $databasename) or die(mysql_error());
$insertGoTo = "sucess.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_yourdatabase, $databasename);
$query_query = "SELECT * FROM employee";
$query = mysql_query($query_query, $databasename) or die(mysql_error());
$row_query = mysql_fetch_assoc($query);
$totalRows_query = mysql_num_rows($query);
mysql_free_result($query);
?>
您忘记传递关键字“值”。检查插入到的语法
尚未设置您要传递给MySQL的变量(“$name”、“$password”、“$email”、“$contact”、“$age”、“$gender”、“$location”、“$skill”)(它们没有值)
您没有处理空字符串的情况
只有在验证了所有参数(可能存在SQL注入漏洞*)后,才应调用数据库
如果您正在学习PHP,我建议您不要使用双引号
最好将输出与逻辑分开,这样以后会更易于维护
这里有图书馆
*:您可能还想替换一些字符,PHP中有用于此的函数,例如htmlentities和mysql\u real\u escape\u string
您忘记传递关键字“值”。检查插入到的语法
尚未设置您要传递给MySQL的变量(“$name”、“$password”、“$email”、“$contact”、“$age”、“$gender”、“$location”、“$skill”)(它们没有值)
您没有处理空字符串的情况
只有在验证了所有参数(可能存在SQL注入漏洞*)后,才应调用数据库
如果您正在学习PHP,我建议您不要使用双引号
最好将输出与逻辑分开,这样以后会更易于维护
这里有图书馆
*:您可能也想替换一些字符,PHP中有用于此的函数,例如htmlentities和mysql\u real\u escape\u string。我想可能是有问题。我会再次添加它并给你链接。当你需要插入代码时,只需按enter键并在每行代码前加上4个空格…可能重复@pcsi:请不要对我的帖子投反对票。我匆忙按了“Ctlr+V”忘记粘贴了。@Ecko但我没有否决你的邮差。我想是有问题吧。我会再次添加它并给你链接。当你需要插入代码时,只需按enter键并在每行代码前加上4个空格…可能重复@pcsi:请不要对我的帖子投反对票。我忘了快速按“Ctlr+V”来粘贴它。@Ecko,但我没有否决你的博文。我在学习PHP-SQL。所以,我们只是试图从不同的来源搜索代码。@Prateksachan进行了相应的编辑。非常感谢。我将查找这些问题并尝试解决它们。如果我做不到;请尝试帮助我。1更多帮助:结果:“恭喜!所有字段都正常;)”出现在页面的左上角。我只想在“提交”按钮下方的同一页面中显示它。@PrateekSachan如果您希望在不重新加载整个页面的情况下显示它,您必须查看ajax(我建议使用jQuery)。否则,只需将这段代码(处理$_POST的地方)放在表单所在的同一页面中,这样,带有“恭喜!所有字段都正常;”的div就会出现在您所说的“提交”按钮之后。我正在学习PHP-SQL。所以,我们只是试图从不同的来源搜索代码。@Prateksachan进行了相应的编辑。非常感谢。我将查找这些问题并尝试解决它们。如果我做不到;请尝试帮助我。1更多帮助:结果:“恭喜!所有字段都正常;)”出现在页面的左上角。我只想在“提交”按钮下方的同一页面中显示它。@PrateekSachan如果您希望在不重新加载整个页面的情况下显示它,您必须查看ajax(我建议使用jQuery)。否则,只需将此代码(处理$_POST的地方)放在表单所在的同一页面中,这样,带有“恭喜!所有字段都正常;)”的div就会出现在您所说的“提交”按钮之后。非常感谢。我还没有签出表格。我会尽快做的。顺便说一下,我之前也写过一篇类似的文章。但它无法验证任何东西。(它也将获取空白值并将其插入表中)。所以我试着浏览,想出了这个并修改了它。非常感谢。我还没有签出表格。我会尽快做的。顺便说一下,我之前也写过一篇类似的文章。但它无法验证任何东西。(它也将获取空白值并将其插入表中)。所以我试着浏览,想出了这个并修改了它。