Log in

View Full Version : Cant get form to Post to mysql database.



trazix
01-04-2010, 12:01 AM
My head is wrecked at my first attempt at posting to a mysql database.

When i click submit I just get an error message from the "Database.php" script.

I'd be greatful if some one could review my code and see if you and spot any amature mistakes.

All the database names password and usernames are correct so I suspect something wrong in the php code.

Here is my code.

<html>
<head><title>DATABASE SUBMIT FORM</title></head>

<body>



<form method="post" action="Database.php">

Title: <input type="text" name="title" size="30"><br>

Description: <input type="text" name="description" size="30"><br>

Date: <input type="text" name="date" size="30" value=""><br>

Url: <input type="text" name="url" size="35" value="http://www.mywebsitename.com"><br>

Photographer: <input type="text" name="photographer" size="30"><br>


<input type="Submit" value="Submit to Database">

</form>



</body>

</html>



<?php

$title = $_POST['title'];

$description = $_POST['description'];

$date = $_POST['date'];

$url = $_POST['url'];

$photographer = $_POST['photographer'];



mysql_connect("www.mysite.com", "u*******1", "5******d") or die(mysql_error());

mysql_select_db ("database name");

$query = "INSERT INTO K4 (title, description, date, url, photographer) VALUES ('NULL','".title."', '".description."', '".date."', '".url."',

'".photographer."')";

mysql_query($query) or die ('Error updatng the database');

echo " Database Updated With: ".title. " ".description." ".date." ".url." ".photographer."";

?>

I was havng trouble creating a table with phpMyadmin but found this script that done the job for me.


<?php
// Connects to your Database
mysql_connect("www.mysite.com", "u*****1", "5*****d") or die(mysql_error());

mysql_select_db("database name") or die(mysql_error());

mysql_query("CREATE TABLE K4
(
ID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID),
title VARCHAR(30),
description VARCHAR(30),
date VARCHAR(30),
url VARCHAR(30),
Photographer VARCHAR(30)
)");

Print "Your table has been created";
?>

Nile
01-04-2010, 12:16 AM
Make sure your also including a mysql_error() after the mysql_select_db!

Change your $query variable to:


$query = "INSERT INTO K4 (title, description, date, url, photographer) VALUES ('NULL','".$title."', '".$description."', '".$date."', '".$url."',

'".$photographer."')";


And change:


echo " Database Updated With: ".title. " ".description." ".date." ".url." ".photographer."";


To:


echo "Database Updated With: $title $description $date $url $photographer";

Good luck! If it doesn't work, please post the error

trazix
01-05-2010, 01:23 PM
Thanks for your reply Nile, after doing what you said i get the following message

Parse error: syntax error, unexpected T_STRING in /mnt/w0802/d31/s23/b0317026/www/mywebsitename.com/KMC/database/Database2.php on line 17

Not being to familliar with php or mysql is this the correct place to put the error statement.




mysql_select_db ("d60651442") mysql_error();




This is how my code looks now.


<?php

$title = $_POST['title'];

$description = $_POST['description'];

$date = $_POST['date'];

$url = $_POST['url'];

$photographer = $_POST['photographer'];



mysql_connect("www.mywebsitename.com", "u*******1", "5******d") or die(mysql_error());

mysql_select_db ("d60651442") mysql_error();


$query = "INSERT INTO K4 (title, description, date, url, photographer) VALUES ('NULL','".$title."', '".$description."', '".$date."', '".$url."',

'".$photographer."')";

mysql_query($query) or die ('Error updatng the database');

echo "Database Updated With: $title $description $date $url $photographer";

?>

fobos
01-27-2010, 07:20 AM
try this code for your database.php.


<?php
$con = mysql_connect("localhost","u*******1","5******d");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("d60651442", $con);
$sql="INSERT INTO K4 (title, description, date, url, photographer)
VALUES ('$_POST[title]','$_POST[description]','$_POST[date]','$_POST[url]','$_POST[photographer]')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
?>


This is what i use for my insert.

liamallan
02-16-2010, 08:49 PM
Try This:
Form:

<form method="post" action="Database.php">
Title: <input type="text" name="title" size="30"><br>
Description: <input type="text" name="description" size="30"><br>
Date: <input type="text" name="date" size="30" value=""><br>
Url: <input type="text" name="url" size="35" value="http://www.mywebsitename.com"><br>
Photographer: <input type="text" name="photographer" size="30"><br>
<input type="Submit" value="Submit">
</form>
Database.php:

<?php
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company
$db_user = "db_username"; // change to your database username
$db_password = "password"; // change to your database password
$database = "db_name"; // provide your database name
$db_table = "table_name"; // provide table name


$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>
<HTML>
<HEAD>
<TITLE>PAGE_TITLE</TITLE>
</HEAD>
<body>
<p>
<?php
if (isset($_REQUEST['Submit'])) {
# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE
$sql = "INSERT INTO $db_table(name,description,date,url,photographer) values ('".mysql_real_escape_string(stripslashes($_REQUEST['name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['description']))."','".mysql_real_escape_string(stripslashes($_REQUEST['date']))."','".mysql_real_escape_string(stripslashes($_REQUEST['url']))."','".mysql_real_escape_string(stripslashes($_REQUEST['photographer']))."')";
if($result = mysql_query($sql ,$db)) {
echo '<h1>Thank you</h1>Your details been added to our database<br><br>;

echo "<a href='index.php'>Back to Index</a>"; // page to be took back to after submission
} else {
echo "ERROR: ".mysql_error();
}
} else {
?>
<form method="post" action="Database.php">
Title: <input type="text" name="title" size="30"><br>
Description: <input type="text" name="description" size="30"><br>
Date: <input type="text" name="date" size="30" value=""><br>
Url: <input type="text" name="url" size="35" value="http://www.mywebsitename.com"><br>
Photographer: <input type="text" name="photographer" size="30"><br>
<input type="Submit" value="Submit">
</form>
<?php
}
?>
</body>
</html>