简体   繁体   中英

php not inserting data into mysql database

I have the following code:

<?php
  include("phpconnect.php");

  $name = $_GET["name"];
  $date = $_GET["date"];
  echo $name;
  echo $date;

  $sql = "INSERT INTO main (name, visits, visitDate, lastVisit) 
  VALUES ('$name', '1', '$date', '$date')";

?>

When the code runs I get a message from phpconnect.php saying that it successfully connected. However, when I check the database there is no information in it. If anyone knows why this is happening or how I could fix it please let me know. Thanks!

The reason your code is not doing anything is because you haven`t actually executed it yet. Now assuming that the "phpconnect" file establishes a database connection for you. I suggest you check out t his page in case you are using MYsqli check out this one .

If you have any further questions i`d be happy to help.

Your code creates an SQL command but doesn't send it to the database to execute. Exactly how it should do that depends on what happened in phpconnect.php . Assuming that the latter contains something along the lines of....

 <?php
 ...
 $dbh=mysqli_connect($host, $user, $pass, $database);
 ...

Then your script should end with...

 $result=mysqli_query($dbh, $sql); // this sends your command to the DBMS
 if (false===$result) {  // because you should always check the outcome
    print mysqli_error($dbh);
 } else {
    print "added " . mysqli_num_rows($result) . " rows";
 }
 mysqli_close($dbh); // not required but good practice

 ?>

You need to take some time to learn about SQL Injection before putting your code on the internet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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