简体   繁体   中英

Inserting into mySQL with PHP but nothing shows up in phpmyadmin

While trying to insert into a mySQL database I get no errors but nothing shows up in the phpmyadmin query. Here is the code for the php file that I am trying to insert with. I know this isn't the best way to do it but it's what I have so far.

<?php

require 'steamauth/steamauth.php';
require 'php/main.php';
require 'php/db.php';

$title = $_POST["title"];
$desc = $_POST["desc"];
$date = $_POST["date"];
$skills = $_POST["skills"];
$budget = $_POST["budget"];
$tc = $_POST["tc"];
$steamid = $steamprofile['steamid'];
$steamname = $steamprofile['personaname'];
$avatar = $steamprofile['avatarmedium'];
$dateposted = date("m-d-y");


// insert into db

if(1==1){

  $insert = $link->query("INSERT INTO livepost (title, desc, date, skills, budget, tc, steamid, steamname, avatar, dateposted)
  VALUES ('$title', '$desc', '$date' , '$skills' , '$budget', '$tc', '$steamid', '$steamname', '$avatar', '$dateposted')");

  header('Location: index');

} else {

  header('Location: index?insertdidntwork');

}

date and desc are keywords in mysql. You have to escape the column names:

  $insert = $link->query("INSERT INTO livepost (title, `desc`, `date`, skills, budget, tc, steamid, steamname, avatar, dateposted)
  VALUES ('$title', '$desc', '$date' , '$skills' , '$budget', '$tc', '$steamid', '$steamname', '$avatar', '$dateposted')");

Hint 1: learn about prepred statemts7

Hint 2: check for Errors after executing SQL Statements.

Hint 3: your if elsestatement is not checking if the insert works.

@Jens already pointed about keywords .

Using Prepared Statements.

<?php
if(1==1){

  $insert = $link->prepare("INSERT INTO livepost (title, `desc`, `date`, skills, budget, tc, steamid, steamname, avatar, dateposted) VALUES (?,?,?,?,?,?,?,?,?,?)");
  $insert->bind_param("sss",$title,$desc,$date,$skills,$budget,$tc,$steamid,$steamname,$avatar,$dateposted );
  $insert->execute();

  header('Location: index');

} else {

  header('Location: index?insertdidntwork');

}

May be what I think problem is in this line. $dateposted = date("mdy");

$dateposted = date("Ymd"); date should be in Ymd format.

Try once like this. And, Please respond back.

maybe you can try this one and you should always use $mysqli->error() function to know what type of error you have

   <?php
$title = $_POST["title"];
$desc = $_POST["desc"];
$date = $_POST["date"];
$skills = $_POST["skills"];
$budget = $_POST["budget"];
$tc = $_POST["tc"];
$steamid = $steamprofile['steamid'];
$steamname = $steamprofile['personaname'];
$avatar = $steamprofile['avatarmedium'];
$dateposted = date("m-d-y");
// insert into db
if (1 == 1) {
    $insert = $link ->query("INSERT INTO livepost (`title`,`desc`,`date`,`skills`,`budget`,`tc`,`steamid`,`steamname`,`avatar`,`dateposted`)

    VALUES ('".$title."', '".$desc."', '".$date."' , '".$skills."' , '".$budget."', '".$tc."', '".$steamid."', '".$steamname."', '".$avatar."', '".$dateposted."')") or die($mysqli -> error . __LINE__);
    header('Location: index');
} else {
    header('Location: index?insertdidntwork');
}
?>

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