使用php在mysql中插入数据

时间:2016-05-03 20:55:33

标签: php mysql

我正在尝试将数据添加到MYSQL数据库中。我研究它http://www.tutorialrepublic.com/php-tutorial/php-mysql-insert-query.php

它,看看我是否正确地获得了我的语法和参数,但是当你在我的MYSQL中没有提交表单时。

这是php:

       <?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost","warren","uikahghi","warren");//This is the login creditial 

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());//Error check for execution 
}


// Escape user inputs for security
$contactname =  mysqli_real_escape_string($link, $_POST['contactname']);//gender variable
$age =  mysqli_real_escape_string($link, $_POST['age']);//gender variable
$height =  mysqli_real_escape_string($link, $_POST['height']);//gender variable
$weight =  mysqli_real_escape_string($link, $_POST['weight']);//gender variable
$membership =  mysqli_real_escape_string($link, $_POST['membership']);//gender variable
$gender =  mysqli_real_escape_string($link, $_POST['gender']);//gender variable
$email =  mysqli_real_escape_string($link, $_POST['email']);//gender variable


// attempt insert query execution
if (!empty($contactname) && !empty($age) && !empty($weight) && !empty($height)  && !empty($gender)  && !empty($email)){ 

$sql = "INSERT INTO customer(customer_id, contactname, age, weight, height, gender, email) 
VALUES ('0', '$contactname' , '$age', '$gender', '$weight', '$height', '$email')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);
}
?>

这是html:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<body>
<form action="test.php" method="post">
<div class="formblock">
                            <label class="screen-reader-text"><h5>Name</h5></label>
                            <input type="text" name="contactname" id="contactname" value=" " class="txt requiredField" placeholder="Name:" />

                            </div>

                        <div class="formblock">
                <label class="screen-reader-text"><h5>Age</h5></label>
                <input type="text" name="age" id="age" value=" " class="txt requiredField" placeholder="Age:" />    
                        </div>


                        <div class="formblock">
                <label class="screen-reader-text"><h5>Weight</h5></label>
                <input type="text" name="weight" id="weight" value=" " required=" What Your weight? " class="txt requiredField" placeholder="Weight:" />
                </div>

                        <div class="formblock">
                <label class="screen-reader-text"><h5>Height</h5></label>
                <input type="text" name="height" id="height" value=" " class="txt requiredField" placeholder="Height:" />
                    </div>

                        <div class="clearfix"></div>
                        <div class="formblock">
                            <label class="screen-reader-text"><h5>Email</h5></label>
                            <input type="text" name="email" id="email" value=" " class="txt requiredField email" placeholder="Email:"/>
                                </div>
            <div class="formblock">
                <label class="screen-reader-text"><h5>Membership</h5>
                    <input type="radio" name="membership" value="Gold" value=" "  class="txt requiredField" placeholder="Gold"/>Gold<br>
                    <input type="radio" name="membership" value="Silver" value=" " class="txt requiredField" placeholder="Silver"/>Silver
                    <input type="radio" name="membership" value="Bronze" value=" " class="txt requiredField" placeholder="Bronze"/>Bronze
                        </label></div>

            <div class="clearfix"></div>
            <div class="formblock">
                <label class="screen-reader-text"><h5>Gender</h5>                                           
                    <input type="radio" name="gender" value="Male" class="txt requiredField" placeholder="Male"/>Male
                    <input type="radio" name="gender" value="Female" class="txt requiredField" placeholder="Female"/> Female</label>
                </div>



                        <div class="clearfix"></div>
                        <div class="clearfix"></div>  
                            <button name="Submit" type="Submit" class="subbutton"><h5>Next Trainers</h5></button>
                            <input type="hidden" name="Submit" id="Submit" value="true" />      
                    </div>

</form>
</body>
</html>

最终,我想在MYSQL中使用两个表单页面和三个表格,但我正在做这个。

请帮帮我吗?

1 个答案:

答案 0 :(得分:0)

不要将变量放在单引号中,或者假设您将一个值为“$ age”的字符串传递给它,而不是输入。请注意,在您的示例中,VALUES部分中的1不是单引号,因为它是整数,而不是字符串。因此,如果您尝试将这些字符串放入数据库中,让我们说它正在寻找一个整数,那么每次都会失败。与INSERT INTO'客户'相同的问题。这里不需要单引号。下次提供您收到的错误。找出失败的东西会更容易。

$sql = "INSERT INTO customer(customer_id, contactname, age, weight, height, gender, email) 
VALUES (0, $contactname, $age, $gender, $weight, $height, $email)";

这可能会解决您未在数据库中输入任何内容的问题。只需从这些行中删除所有单引号。