Reputation: 13
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');
}
Upvotes: 1
Views: 644
Reputation: 26
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');
}
?>
Upvotes: 0
Reputation: 10548
@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("m-d-y");
$dateposted = date("Y-m-d");
date should be in Y-m-d
format.
Try once like this. And, Please respond back.
Upvotes: 0
Reputation: 69460
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.
Upvotes: 3