PHP SQL表单插入

时间:2012-06-01 04:35:41

标签: php sql

我开发了一个可以在数据库中插入很多东西的表单。但不知何故,当页面填满时,它只插入数据库管理员的user_password。我该如何解决这个问题?

<?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?>

2 个答案:

答案 0 :(得分:2)

  1. 您忘记传递关键字“VALUES”。检查INSERT INTO的语法。
  2. 您传递给MySQL的变量('$ name','$ password','$ email','$ contact','$ age','$ gender','$ location','$ skill' )尚未设置(它们没有值)。
  3. 您没有处理空字符串的情况。
  4. 如果您已经验证了所有参数(您可能有SQL注入漏洞*),则只应调用数据库。
  5. 如果你正在学习PHP,我建议不要使用双引号。
  6. 将输出与逻辑分开可能会更好,以后会更容易维护。
  7. 这里有图书馆。
  8. *:您可能也希望浏览一些字符,PHP中有函数,例如htmlentities和mysql_real_escape_string。

答案 1 :(得分:1)

我为你写了一个完整的代码:

<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">&nbsp;</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>&nbsp;</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);
?>

希望它有所帮助!