PHP Programming > Beginners - Learning PHP
Uploading data to Mysql database using html form trouble
mdahlke:
I'm trying to use a form for users to upload simple text to but am not having any luck doing so. Here is what I have so far but I do not know what's going wrong.
First is the form...
--- PHP Code: --- <form action="connect.php" method="post">
Posting Title:
<input type="text" name="title" placeholder="Posting Title" /><br>
Location of Sale:
<input type="text" name="location" placeholder="Location of Sale" /><br>
Time of Sale:
<input type="datetime-local" name="when" placeholder="Time of Sale" /><br>
Main Items of Sale:
<input type="text" name="mainitems" placeholder="Main Items of Sale" /><br>
<input type="submit" value="Post It!"/>
</form>
--- End code ---
next is the 'connect.php' file...(I have left out the information about the database information but have checked multiple times to verify that it is correct. If there is nothing wrong in the codes I have provided I will double check the database information)...
--- PHP Code: ---$title=$_POST['title'];
$location=$_POST['location'];
$when=$_POST['when'];
$mainitems=$_POST['mainitems'];
mysql_connect($localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$sql = "INSERT INTO $table ('title','location','when','mainitems') VALUES
('','$title','$location','$when','$mainitems')";
$result=mysql_query($sql);
if($result){
echo "<html>
<head>
<title>Post</title>
<link href='layout.css' rel='stylesheet' type='text/css'/>
</head>
<body>
<?php include 'leftSidebar.php' ?>
<section class='container'>
<?php include 'header.php' ?>
<article class='content'>
<a href='listingsPage.php'>Click to see your listing</a>
</article>
<?php include 'footer.php' ?>
</section>
</body>
</html>";
}
else {
echo "ERROR";
}
mysql_close();
?>
--- End code ---
wilson382:
I found your mistake.
--- Quote from: mdahlke on July 09, 2012, 09:16:00 PM ---
instead of this query:
--- PHP Code: ---
$sql = "INSERT INTO $table ('title','location','when','mainitems') VALUES
('','$title','$location','$when','$mainitems')";
--- End code ---
--- End quote ---
it should be like this:
--- PHP Code: ---
$sql = "INSERT INTO $table (title,location,_when,mainitems) VALUES
('$title','$location','$when','$mainitems')";
--- End code ---
before the title value you had ' ' which means no data or null if you want to do that you need to add one more column.
and the big error i found in your query was:
you used "when" for a column name which is a sql reserved word.
if you still want to use when you can use "_when" and update your database structure and it will work fine.
all you need is provide a table_name for the variable $table and the database parameters
I tested on my localhost it work
RaythXC:
--- Quote from: wilson382 on July 10, 2012, 02:29:19 AM ---I found your mistake.
it should be like this:
--- PHP Code: ---
$sql = "INSERT INTO $table (title,location,_when,mainitems) VALUES
('$title','$location','$when','$mainitems')";
--- End code ---
before the title value you had ' ' which means no data or null if you want to do that you need to add one more column.
and the big error i found in your query was:
you used "when" for a column name which is a sql reserved word.
if you still want to use when you can use "_when" and update your database structure and it will work fine.
all you need is provide a table_name for the variable $table and the database parameters
I tested on my localhost it work
--- End quote ---
You could prob get round the reserved word if you do:
--- PHP Code: ---
$sql = "INSERT INTO $table (`title`,`location`,`when`,`mainitems`) VALUES
('$title','$location','$when','$mainitems')";
--- End code ---
mdahlke:
Thank you for the help...Now I can get the form to post and there is no "error" but when I go into the database the new entry is blank in everyfield. Any solutions?
wilson382:
--- Quote from: RaythXC on July 10, 2012, 05:49:32 AM ---You could prob get round the reserved word if you do:
--- PHP Code: ---
$sql = "INSERT INTO $table (`title`,`location`,`when`,`mainitems`) VALUES
('$title','$location','$when','$mainitems')";
--- End code ---
--- End quote ---
thanks for letting me know now i know.
--- Quote from: mdahlke on July 10, 2012, 07:34:12 AM ---Thank you for the help...Now I can get the form to post and there is no "error" but when I go into the database the new entry is blank in everyfield. Any solutions?
--- End quote ---
are you specifying a table name? remember you are using a variable for the table name.
Navigation
[0] Message Index
[#] Next page
Go to full version