插入新记录时创建新表

时间:2016-07-18 09:20:17

标签: php mysql

我有一个注册表单,因为我想在一个数据库中插入一条记录,但同时在点击提交时创建新表...但我面临的问题是“创建”查询不执行所以消息显示为“Something Went Wrong !!! :(”。

<?php
include('data_conn.php');

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

  $email = mysql_real_escape_string( $_POST['email']);
$query = "SELECT * FROM login WHERE email='$email'";
    $result = mysql_query($query) or die(mysql_error());

    if (mysql_num_rows($result) ) {
        echo '<script language="javascript">';
    echo 'alert("Email is Already Exist...."); location.href="signup.php"';
    echo '</script>';
    }
    else {
        $f_name = $_POST['f_name'];

        $c_name = $_POST['c_name'];
        $c_add = $_POST['c_add'];
        $mob = $_POST['mob'];
        $email = $_POST['email'];
        $password = $_POST['password'];



        $query = "INSERT INTO login (first_name,company_name,company_add,last_name,email,password,account) VALUES ('$f_name','$c_name','$c_add','$mob','$email','$password','deactivate')";

       $result = mysql_query($query) or die(mysql_error());

        $query1="CREATE TABLE $db_name.$email('name' VARCHAR(20) NOT NULL)";


$result1=mysql_query($query1);




       if($result==1 && $result1==1)
    {
        echo '<script language="javascript">';
    echo 'alert("successfully registered!!!"); location.href="signup.php"';
    echo '</script>';

    }
    else
    {
      echo '<script language="javascript">';
    echo 'alert("Something Went Wrong!!! :("); location.href="signup.php"';
    echo '</script>';
    }
    }
}
?>

2 个答案:

答案 0 :(得分:0)

   $query1="CREATE TABLE IF NOT EXISTS $db_name.$email('name' VARCHAR(20) NOT NULL)"; 
    $result1=mysql_query($query1); 

好的,你在这里创建了一些表。但是您要将数据插入表login,为什么?如果上面的表是目的地,那么为什么这个login表? 如果您创建了一个新表newtable,请使用它。

 $query = "INSERT INTO newtable(first_name,company_name,company_add,last_name,email,password,account) VALUES ('$f_name','$c_name','$c_add','$mob','$email','$password','deactivate')";
               $result = mysql_query($query) or die(mysql_error());

答案 1 :(得分:0)

//删除表列名称的引号

$query1="CREATE TABLE $db_name.$email(name VARCHAR(20) NOT NULL)";