无法通过php

时间:2016-04-03 09:33:27

标签: php mysql sql

我创建了一个php脚本,它在我的数据库中插入值,但它不起作用,连接中没有错误,数据库中也没有错误。

我也尝试过在phpmyadmin中进行查询,效果很好,请帮我解决一下你可以运行它的代码我已经更改了密码..

<?php
    $con= mysqli_connect("mysql.hostinger.in","u744363236_users","stackoverflow","u744363236_users");
        // Check connection
    if ($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    } 

    $name1 = $_POST['name'];
    $phone1 = $_POST['phone'];
    $password1 = $_POST['password'];
    $sql = "INSERT INTO users (name,phone,password) VALUES ($name1,$phone1,$password1)";
    if(mysqli_query($con,$sql)){        
        echo 'succeed';
    }   
    else{
        echo 'failure';     
    }
    mysqli_close($con);
?>       

1 个答案:

答案 0 :(得分:0)

阿赫亚。像我描述的那样改变。对于$ sql

请勿直接在字符串中使用$ name1,请使用&#39;&#34;。$ name1。&#34;&#39;

<?php
$con= mysqli_connect("localhost","root","","test");
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}


$name1 = "Hirendrasinh";
$phone1 = "9428416590";
$password1 = "Test1234";
$sql = "INSERT INTO users (name,phone,password) VALUES ('".$name1."','".$phone1."','".$password1."')";

if(mysqli_query($con,$sql)){

    echo 'succeed';
}
else{
    echo 'failure';

}
mysqli_close($con);
?>