简体   繁体   English

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

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

"I have read a lot of problem been solved in stackoverflow similar to my problem, and have seen a lot of example, yet still my code is not inserting in to mysql. however if i hard feed the php it would insert. my info is coming as submit from html post.I have good server connection and also connection to the database, can any one help me if i miss any thing. here is my code below." “我已经阅读了很多类似于我的问题在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>

" i expect output of 5/2 to be 2.5" “我希望5/2的输出为2.5”

Write the name for submit button 输入提交按钮的名称

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

then in php file 然后在PHP文件

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

}

this if statement will run 这个if语句将运行

you not given name attribute to button so give name="submit" and if you want to upload file then change type="file" 您没有为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>

You're checking isset($_POST['submit']) but there is no input field which is posted with submit name.. you need to add the name attribute in the submit button. 您正在检查isset($_POST['submit'])但是没有输入字段带有提交名称。.您需要在submit按钮中添加名称属性。 also you're not passing the $connection in the mysqli_query . 你也没有在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>

One more suggestion always use PDO in code to prevent SQL injection. 还有一个建议总是在代码中使用PDO来防止SQL注入。 Your code is vulnerable to sql injection. 您的代码容易受到sql注入的攻击。

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

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