添加列后数据未保存到Mysql数据库

时间:2014-09-23 13:23:27

标签: php mysql

我使用 tutorial

创建了一个简单的注册表单

由于我在表单中添加了一个额外的列姓,因此不会将数据保存在我的sql数据库中

以下是我文件中的代码

为registration.php

<html>
    <head>
        <title> Registration Form</title>
    </head>

<body>
<form method='post' action='registration.php'>
    <table width='400' border='5' align='center'> 
    <tr>    
        <td align='center' colspan='5'><h1>Registation Form</h1></td>
    </tr>

    <tr>    
        <td align='center'>First Name:</td>
        <td><input type='text' name='fname' /></td>
    </tr>


   <tr> 
        <td align='center'>Last Name:</td>
        <td><input type='text' name='lname' /></td>
    </tr>


    <tr>    
        <td align='center'>User Password:</td>
        <td><input type='password' name='pass' /></td>
    </tr>

    <tr>    
        <td align='center'>Email:</td>
        <td><input type='text' name='email' /></td>
    </tr>

    <tr>    
        <td colspan='5' align='center'><input type='submit' name='submit' value='Sign Up' /></td>
    </tr>


    </table>
</form>
<center><b>Already Registered</b><br><a href='login.php'>Login Here</a></center>
</body>
</html>
<?php 
mysql_connect("localhost","root","");
mysql_select_db("users_db");

    if(isset($_POST['submit'])){

     $user_fname = $_POST['fname'];
     $user_lname = $_POST['lname'];

     $user_pass = $_POST['pass'];
     $user_email = $_POST['email'];

    if($user_fname==''){
    echo "<script>alert('Please enter your first name!')</script>";
    exit();
    }

    if($user_lname==''){
    echo "<script>alert('Please enter your last name!')</script>";
    exit();
    }

    if($user_pass==''){
    echo "<script>alert('Please enter your password!')</script>";
    exit();
    }

    if($user_email==''){
    echo "<script>alert('Please enter your email!')</script>";
    exit();
    }

    $check_email = "select * from users where user_email='$user_email'";

    $run = mysql_query($check_email);

    if(mysql_num_rows($run)>0){

    echo "<script>alert('Email $user_email is already exist in our database, plz try another one!')</script>";
    exit();
    }

    $query = "insert into users (user_fname,user_lname,user_pass,user_email) values ('$user_fname','$user_lname',$user_pass','$user_email')";
    if(mysql_query($query)){

    echo "<script>window.open('welcome.php','_self')</script>";

    }       
}

?>

当我从用户运行SHOW COLUMNS时,这是输出

Field
Type
Null
Key
Default
Extra
id
int(10)
NO
PRI
NULL
auto_increment
user_fname
varchar(100)
NO
NULL
user_lname
varchar(100)
NO
NULL
user_pass
varchar(50)
NO
NULL
user_email
varchar(100)
NO
NULL

1 个答案:

答案 0 :(得分:0)

我为你创建了插入记录。试试这个也改变数据库设置。这也是感谢.php表示谢谢或成功。

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_localhost2 = "localhost";
$database_localhost2 = "users_db";
$username_localhost2 = "username";
$password_localhost2 = "password";
$localhost2 = mysql_pconnect($hostname_localhost2, $username_localhost2, $password_localhost2) or trigger_error(mysql_error(),E_USER_ERROR); 
?>
<?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 users (id, user_fname, user_lname, user_pass, user_email) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['user_fname'], "text"),
                       GetSQLValueString($_POST['user_lname'], "text"),
                       GetSQLValueString($_POST['user_pass'], "text"),
                       GetSQLValueString($_POST['user_email'], "text"));

  mysql_select_db($database_localhost2, $localhost2);
  $Result1 = mysql_query($insertSQL, $localhost2) or die(mysql_error());

  $insertGoTo = "Thanks.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Id:</td>
      <td><input type="hidden" name="id" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">User_fname:</td>
      <td><input type="text" name="user_fname" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">User_lname:</td>
      <td><input type="text" name="user_lname" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">User_pass:</td>
      <td><input type="text" name="user_pass" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">User_email:</td>
      <td><input type="text" name="user_email" 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="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>
</html>