mysql将值插入表中?

Posted

技术标签:

【中文标题】mysql将值插入表中?【英文标题】:mysql insert values into table? 【发布时间】:2014-12-18 09:50:23 【问题描述】:

我正在尝试将我的注册表单数据插入到我的表中,但由于某种原因没有插入任何内容并且我没有收到错误。

有人可以告诉我我哪里出了问题,因为我对 phpmysql 真的很陌生,在此先感谢。

我的 config.php 文件保存了我的连接:

<?php
$host="localhost";
$username="mark";
$password="password";
$db_name="hewden1";
$conn = mysql_connect("$host", "$username", "$password") or die("Could Not Connect to Server");
$db = mysql_select_db("$db_name")or die("Cannot Connect the Database"); 
?>

这是我的注册表单html

<form name="test" action="validation/signup_process.php" method="post">
<input type="text" name="compname" class="login_form" placeholder="Company or Trading Name">
<br>
<input type="text" name="contactname" class="login_form" placeholder="Contact Name"><br><br><br>
<input type="text" name="emailaddress" class="login_form" placeholder="Email Address">
<br>
 <input type="text" name="password1" class="login_form" placeholder="Password">
 <input type="text" name="password2" class="login_form" placeholder="Confirm Password"><br>

 <input type="submit" name="submit" value="Register" class="buttons">
</form> 

我的 php/MySQL 代码:

<?php 
session_start();
include("config.php");
//retrieve our data from POST
$compname = $_POST['compname'];
$contactname = $_POST['contactname'];
$username = $_POST['emailaddress'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];

if($password1 != $password2) 
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Password&#39;s did not match.</p> </div>';
header("location:..\sign-up.php");

else
if(strlen($username) > 30) 
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Username you have selected is incorrect.</p> </div>';
header("location:..\sign-up.php");

else

$hash = hash('sha256', $password1);

function createSalt()
$text = md5(uniqid(rand(), true));
return substr($text, 0, 3); 
$salt = createSalt();
$password = hash('sha256', $salt . $hash);

$username = mysql_real_escape_string($username);
$query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ('$contactname','$compname','$username', '$salt', now(), 'visitor');" or die(mysql_error());

echo "eveything ok";

 ?>

【问题讨论】:

您正在创建一个包含查询的变量,但从未真正执行它。 【参考方案1】:

您忘记执行查询。您需要执行查询才能插入数据库。

试试这个

    <?php 
    session_start();
    include("config.php");
    //retrieve our data from POST
    $compname = $_POST['compname'];
    $contactname = $_POST['contactname'];
    $username = $_POST['emailaddress'];
    $password1 = $_POST['password1'];
    $password2 = $_POST['password2'];

    if($password1 != $password2) 
    $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Password&#39;s did not match.</p> </div>';
    header("location:..\sign-up.php");

    else
    if(strlen($username) > 30) 
    $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Username you have selected is incorrect.</p> </div>';
    header("location:..\sign-up.php");

    else

    $hash = hash('sha256', $password1);

    function createSalt()
    $text = md5(uniqid(rand(), true));
    return substr($text, 0, 3); 
    $salt = createSalt();
    $password = hash('sha256', $salt . $hash);

    $username = mysql_real_escape_string($username);
    $query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ('$contactname','$compname','$username', '$salt', now(), 'visitor')";
    mysql_query($query); //You forgot to add this line
    echo "eveything ok";

     ?>

【讨论】:

我尝试将错误行添加到我的查询中,但它产生的只是“错误”我也再次尝试了你的代码,那次我仍然没有错误,仍然没有任何东西插入到我的表中 您能告诉我插入查询中的visitor 是什么吗?【参考方案2】:

使用这个

$host="localhost";
$username="mark";
$password="password";
$db_name="hewden1";
$conn = mysql_connect($host, $username, $password) or die("Could Not Connect to Server");
$db = mysql_select_db($db_name)or die("Cannot Connect the Database"); 

//插入查询

$query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ('$contactname','$compname','$username', '$salt', now(), 'visitor');" or die(mysql_error());
$res = mysql_query($query);

【讨论】:

这似乎没有任何区别,我的表中仍然没有插入任何内容 @John Cleeves Try: if($res)echo "ok"; else mysql_error($res) @Joci93 这是我的错误 --- > 警告:mysql_error() 期望参数 1 是资源,在 C:\xampp\htdocs\hewden\ssa\suppliers\validation\signup_process 中给出的布尔值。 php 在第 36 行一切正常【参考方案3】:

您的插入查询在一个函数中,我没有看到它被调用。 然后,您的$query 也不会被执行。 在您创建查询的行之后添加mysql_query($query);

【讨论】:

【参考方案4】:

Hey Only 像这样更改查询

$query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ($contactname,$compname,$username, $salt, now(), 'visitor');" or die(mysql_error());

我认为它会正常工作

然后查看类似的答案

Inserting $variable or $_POST value into mysql table

【讨论】:

【参考方案5】:

试试这个:

<?php 
session_start();
include("config.php");
//retrieve our data from POST
$compname = $_POST['compname'];
$contactname = $_POST['contactname'];
$username = $_POST['emailaddress'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];

if($password1 != $password2) 
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Password&#39;s did not match.</p> </div>';
header("location:..\sign-up.php");

elseif($username != "something")   //Write a code in the (). 
if(strlen($username) > 30) 
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Username you have selected is incorrect.</p> </div>';
header("location:..\sign-up.php");

else

$hash = hash('sha256', $password1);

function createSalt()
$text = md5(uniqid(rand(), true));
return substr($text, 0, 3); 
$salt = createSalt();
$password = hash('sha256', $salt . $hash);

$username = mysql_real_escape_string($username);
$query = "INSERT INTO supplier_pre_sign (contactname, company_name, supplier_email, password, date, user_type) VALUES ($contactname,$compname,$username, $salt, now(), 'visitor');" or die(mysql_error());

$res = mysql_query($query);

if($res)echo "inserted"; else mysql_error($res) 

    echo "eveything ok";

 ?>

【讨论】:

我正在编辑代码。您需要在 elseif( ) 部分编写代码。 感谢我使用了这段代码,但仍然没有结果被插入到表格中 同样的事情,我得到的只是最终结果 echo "everything ok";没有错误,也没有插入任何内容

以上是关于mysql将值插入表中?的主要内容,如果未能解决你的问题,请参考以下文章

如果值不存在,将值插入 MySQL 表?

无法将值插入表中,外键约束不断失败

将值插入具有DATETIME列的表中

如何使用java变量将值插入mysql表?

在 python2.7 中使用 pool.apply_async 不将值插入 MySQL 表

如何将值插入到python中的嵌套表中?