简体   繁体   中英

Inserting into a MySQL using PHP

I've been working on this problem for two hours. I'm most likely just missing something incredibly obvious. Basically, I can't get either of these queries to actually insert into the server. I've added the data to the server manually, and it gives no problems. My other scripts are successfully adding things to the server. I've used the ping function to confirm I'm connected to the server. And I've also gone through the list of inputs to make sure nothing unexpected is being added.

I don't know what else to try.

$Name = $_POST['Project_Name'];
$Description = $_POST['Description'];
$Languages = $_POST['Languages'];
$dateposted = $_POST['DatePosted'];
$Spots = $_POST['Spots'];
$Comments = $_POST['Comments'];
$PostingUser = $_SESSION['UserID'];

if(!empty($Name) AND !empty($dateposted))
{
    mysql_query("
INSERT INTO Project
(ProjName
, Slots
, CurrentSlots
, Date_Posted
, Languages
, ProjDescription
, ProjType
, Comments
, AddingUser
) VALUES
('$Name'
, '$Spots'
, '0'
, '$dateposted'
, '$Languages'
, '$Description'
, NULL
, '$Comments'
, '$PostingUser')
");
    mysql_query("INSERT INTO PostedBy(fk_pb_ProjName, fk_pb_Username)VALUES('$Name', '$PostingUser')");
    header("refresh: 1; url=Add-Project.html");
    echo " Project Submitted. Returning to main list.";
}
?>

That's the PHP code, and here's the two SQL tables.

Project:

CREATE TABLE Project
(
  ProjName VARCHAR(100) NOT NULL,
  Slots INT NOT NULL,
  CurrentSlots INT,
  Date_Posted VARCHAR(10) NOT NULL,
  Languages VARCHAR(40),
  ProjDescription VARCHAR(255),
  ProjType VARCHAR(10),
  Comments VARCHAR(255),
  AddingUser VARCHAR(7),
  FOREIGN KEY (AddingUser) references Users(Username),
  PRIMARY KEY(ProjName)
);

PostedBy:

CREATE TABLE PostedBy
(
  fk_pb_ProjName VARCHAR(100),
  fk_pb_Username VARCHAR(7) NOT NULL,
  PRIMARY KEY (fk_pb_ProjName),
  FOREIGN KEY (fk_pb_Username) references Users(Username),
  FOREIGN KEY (fk_pb_ProjName) references Project(ProjName)
);

This is my first time doing anything substantial with PHP and MySQL, so I'm most likely overlooking something incredibly simple.

OLD

 mysql_query("INSERT INTO Project(ProjName, Slots, CurrentSlots, Date_Posted, Languages, ProjDescription, ProjType, Comments, AddingUser)VALUES('$Name', '$Spots', '0', $dateposted, '$Languages', '$Description', NULL, '$Comments', '$PostingUser')");

AND NEW

mysql_query("INSERT INTO Project(ProjName, Slots, CurrentSlots, Date_Posted, Languages, ProjDescription, ProjType, Comments, AddingUser)VALUES('$Name', '$Spots', '0', ------->'$dateposted'<-----, '$Languages', '$Description', NULL, '$Comments', '$PostingUser')");

Be sure to put the single quotes over every variable that you are going to query! i think thats the problem anyways.. Go to your date posted variable this is where the problem should live

Look at this line:

 mysql_query("INSERT INTO PostedBy(fk_pb_Name, fk_pb_Username)VALUES('$Name', '$PostingUser')"

and in your table definition:

fk_pb_ProjName VARCHAR(100),

These do not match.

Apart from that: (after switching to mysqli or PDO) read up on displaying MySQL errors and prin them out.

EDIT: For mysqli, check : http://php.net/manual/en/mysqli.error.php

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