php - mysql 将值插入表中?

标签 php html mysql

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

有人可以告诉我哪里出了问题吗,因为我对 php 和 MySQL 很陌生,提前谢谢。

我的 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";

} }?>

最佳答案

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

试试这个:

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

    } }?>

关于php - mysql 将值插入表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27543617/

相关文章:

php - 成功登录后如何将用户重定向到新的 PHP 页面?

php - 向Youtube API的PHP POST请求:获取服务器错误

php - php mysql 列的总和不起作用

php - 获取受影响的行数,包括未更改的行数

php - 忽略特定的 WHERE 标准

jQuery 隐藏/显示不能很好地与@media print 配合使用

c# - 使用 MySQLDataAdapter 输入字符串的格式不正确

HTML 相对路径导致导航菜单中出现 404 错误

html - 如何在导航菜单按钮中组织 3 个元素?

php - 如何在 MySQL 数据库中获取枚举可能值?