【问题标题】:"php not inserting in to mysql server in xampp"“php没有插入到xampp中的mysql服务器”
【发布时间】:2019-07-13 04:48:46
【问题描述】:

“我已经阅读了很多在 stackoverflow 中解决的与我的问题类似的问题,并且已经看到了很多示例,但我的代码仍然没有插入到 mysql 中。但是,如果我硬输入 php,它会插入。 “

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="image";


// Create connection

$connection = mysqli_connect($servername, $username, $password, $db); // Establishing Connection with Server
if (!$connection) {
      die("Connection failed: " . mysqli_connect_error());
    }
else{
      echo "Connected successfully"; 
    }


if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$name = $_POST['name'];
$image = $_POST['image'];

echo $name;
echo $image;

if($name !=''||$image !=''){
//Insert Query of SQL
$query = mysqli_query("INSERT INTO image (id, name, imagename) VALUES ('NULL', '$name', '$image')");
echo "Data Inserted successfully...!!";
}
else{
echo "Insertion Failed <br/> Some Fields are Blank....!!";
}
}
mysqli_close($connection); // Closing Connection with Server
?>


<form action = "test2.php" method="POST" enctype="multipart/form-data">
    <label>name: </label><input type="text" name="name" />
    <label>File: </label><input type="text" name="image" />
    <input type="submit" />
</form>
</body>
</html>

" 我希望 5/2 的输出为 2.5"

【问题讨论】:

标签: php html sql


【解决方案1】:

写下提交按钮的名称

<input type="submit" name="submit" />

然后在php文件中

if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL

}

这个 if 语句将运行

【讨论】:

  • 但我认为这是通过设置此表单在 HTML 中所做的事情
  • "
    "跨度>
  • 你可以看到 我认为这里定义了类型和名称以在 php 脚本中调用。
【解决方案2】:

你没有给name属性button所以给name="submit"如果你想上传文件然后改变type="file"

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="image";


// Create connection

$connection = mysqli_connect($servername, $username, $password, $db); // Establishing Connection with Server
if (!$connection) {
      die("Connection failed: " . mysqli_connect_error());
    }
else{
      echo "Connected successfully"; 
    }


if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$name = $_POST['name'];
$image = $_POST['image'];

echo $name;
echo $image;

if($name !=''||$image !=''){
//Insert Query of SQL
$query = mysqli_query("INSERT INTO image (id, name, imagename) VALUES ('NULL', '$name', '$image')");
echo "Data Inserted successfully...!!";
}
else{
echo "Insertion Failed <br/> Some Fields are Blank....!!";
}
}
mysqli_close($connection); // Closing Connection with Server
?>


<form action = "test2.php" method="POST" enctype="multipart/form-data">
    <label>name: </label><input type="text" name="name" />
    <label>File: </label><input type="file" name="image" />
    <input type="submit" name="submit" />
</form>
</body>
</html>

【讨论】:

    【解决方案3】:

    您正在检查isset($_POST['submit']),但没有使用提交名称发布的输入字段。您需要在submit 按钮中添加名称属性。你也没有通过mysqli_query中的$connection

        $servername = "localhost";
        $username = "root";
        $password = "";
        $db="image";
    
    
        // Create connection
    
        $connection = mysqli_connect($servername, $username, $password, $db); // Establishing Connection with Server
        if (!$connection) {
              die("Connection failed: " . mysqli_connect_error());
            }
        else{
              echo "Connected successfully"; 
            }
    
    
        if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
        $name = $_POST['name'];
        $image = $_POST['image'];
    
        echo $name;
        echo $image;
    
        if($name !=''||$image !=''){
        //Insert Query of SQL
        $query = mysqli_query($connection, "INSERT INTO image (id, name, imagename) VALUES ('NULL', '$name', '$image')");
        if($query !== false){
            echo "Data Inserted successfully...!!";
        }
        else{
            echo "Query failed";
        }
        }
        else{
        echo "Insertion Failed <br/> Some Fields are Blank....!!";
        }
        }
        mysqli_close($connection); // Closing Connection with Server
        ?>
    
    
        <form action = "test2.php" method="POST" enctype="multipart/form-data">
            <label>name: </label><input type="text" name="name" />
            <label>File: </label><input type="text" name="image" />
            <input type="submit" name = "submit" />
        </form>
        </body>
        </html>
    

    还有一个建议总是在代码中使用 PDO 来防止 SQL 注入。您的代码容易受到 sql 注入的攻击。

    【讨论】:

    • 谢谢,是的,你是对的,我是 php 编码的新手,但更多的是 shell 脚本编码器,但是 sql 插入的编码使用 shell 脚本更复杂,但想使用 php 作为桥梁跨度>
    • 这里即使通过连接和sql,也会有相同的效果。if(!mysqli_query($con,$sql)) if ($con->query($sql) == = TRUE) { echo '新记录已更新'; } else { echo '没有插入数据'; }
    • @eahubs 是的.. 你也可以这样做.. 但是有一个问题,当你执行 select 语句时它会返回 mysqli_result .. 在这个你只是插入所以没关系.如果您将执行 if ($con->query($sql) === TRUE) 检查 select 语句,那么它将失败..因为它在返回 mysqli_result 时不会返回 true 并且您正在通过严格输入检查 true ..
    猜你喜欢
    • 2018-06-06
    • 2013-05-19
    • 2013-06-18
    • 2018-01-09
    • 2017-03-07
    • 2014-09-08
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多