繁体   English   中英

“ php不在xampp中插入mysql服务器”

[英]“php not inserting in to mysql server in xampp”

“我已经阅读了很多类似于我的问题在stackoverflow中解决的问题,并且看到了很多示例,但是我的代码仍未插入mysql。但是,如果我硬喂它会插入php。我的信息是来自html帖子的提交。我有良好的服务器连接以及与数据库的连接,如果我错过任何事情,任何人都可以帮助我。这是下面的代码。”

<?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”

输入提交按钮的名称

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

然后在PHP文件

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

}

这个if语句将运行

您没有为button指定name属性,因此请提供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>

您正在检查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注入的攻击。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM