Uploading data to Mysql database using html form trouble

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]

           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>[/php]

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]$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 "

Post <?php include 'leftSidebar.php' ?> <?php include 'header.php' ?> Click to see your listing <?php include 'footer.php' ?> ";

}

else {
echo “ERROR”;
}

mysql_close();
?>
[/php]

I found your mistake.

it should be like this:
[php]
$sql = “INSERT INTO $table (title,location,_when,mainitems) VALUES
(’$title’,’$location’,’$when’,’$mainitems’)”;
[/php]
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

You could prob get round the reserved word if you do:

[php]
$sql = “INSERT INTO $table (title,location,when,mainitems) VALUES
(’$title’,’$location’,’$when’,’$mainitems’)”;
[/php]

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?

thanks for letting me know now i know.

are you specifying a table name? remember you are using a variable for the table name.

Yes, I my database and table are stored in a variable. It creates a new entry but they are all blank, could it be something with the collation?

how weird because i tested with the same form you provided i dint change anything to the form and it worked.
it added new record with the data i put on the form

provide me with more codes of you project maybe i can help be sure you to replace any sensitive information with **** like this

I’ve included pretty much everything that is needed for the form and database

[php]$localhost=“localhost”;
$username=“ThisIsWhereMyUsernameForTheDatabaseIs;
$password=”*******";
$database=“sale”;
$table=“posting”;

$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 "

Post <?php include 'leftSidebar.php' ?> <?php include 'header.php' ?> Click to see your listing <?php include 'footer.php' ?> ";

}

else {
echo “ERROR”;
}

mysql_close();
?>
[/php]

sale-posting.php

[php]

Sell My Shit
<?php require_once 'leftSidebar.php' ?>
<?php require_once 'header.php' ?>

 <article class="content">

	<article class="home">
	
    	<p>Popular Yard sales going on now!</p>
    
    </article>

</article>

<?php require_once 'footer.php' ?>
[/php]

in the real code the " is not missing at the end of this line…

[php]$username="ThisIsWhereMyUsernameForTheDatabaseIs;[/php]

[php]
$sql = “INSERT INTO $table (title, location, _when, mainitems) VALUES
(’$title’,’$location’,’$when’,’$mainitems’)”;
[/php]

Try replacing with…

[php]
$sql = “INSERT INTO {$table} (title, location, _when, mainitems) VALUES
(’$title’,’$location’,’$when’,’$mainitems’);”;
[/php]

Doing that brought back the “ERROR” being displayed…

Use this code instead

[php]
$sql = “INSERT INTO posting (title, location, _when, mainitems) VALUES
(’$title’,’$location’,’$when’,’$mainitems’);”;
[/php]

When i get home i will text yuor code

Could it be something with my table?

CREATE TABLE `posting` ( `title` varchar(50) NOT NULL, `location` varchar(100) NOT NULL, `_when` varchar(25) NOT NULL, `mainitems` varchar(150) NOT NULL )

no man your table structure is fine.

try this this:

name this file: sale_posting.php, if you name it somethig else remember to update in your html form.
[php]

<?php $localhost="localhost"; $username="*******"; $password="******"; $database="******"; $table="posting"; $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 " Post <?php include 'leftSidebar.php' ?> <?php include 'header.php' ?> Click to see your listing <?php include 'footer.php' ?> ";

}

else {
echo “ERROR”;
}

mysql_close();

?>
[/php]

html form

<html>
<body>
<form action="sale_posting.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>

</body>
</html>
			

back up your files and try only using my file to see if it works.
it should definitely work because on my localhost it works,

There must be something wrong with my hosting provider cause that still didn’t work…

I ran this query from phpmyadmin and everything worked as it should so it must be something in my script, correct?

[php]INSERT INTO cities(city,state,zip) VALUES (‘Madison’,‘WI’,‘53703’)[/php]

Is it still partial working like inserting but blank fields?

Are you getting data from the Post form?

try to print each variable to see if they contains the value you input in the form.

Maybe that’s why you got blank fields once

I echo-ed the $sql and this is what is returned

[php]INSERT INTO posting (title,location,_when,mainitems) VALUES (’’,’’,’’,’’)[/php]

why are my values empty?..this is very frustrating

YEAH SINCE you told me it inserted but it insert blank record i knew it had to do something with the data.

when you submit the form you php script is not getting value sent using POST.

try looking for mispelling on the name of textbox and $_POST[‘check this name’];

what error(s) are you getting now?

Sponsor our Newsletter | Privacy Policy | Terms of Service