PDA

View Full Version : PHP Post form used to work - isnt anymore.


Adam_C
02-20-2010, 08:35 PM
I have this PHP code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SAS | Home</title>
<link href="css/main.css" type="text/css" media="screen" rel="stylesheet" />
</head>
<body>
<div id="container_outer">
<div id="container_inner">
<div id="header"><a href="index.php"><img src="images/nav_bar/logo.png" title="Home Page" border="0" /></a></div>
<div id="category">
<ul>
<li><a href="add-comment.php" title="Add A Comment">Add Story</a></li>
<li><a href="storyhome.php" title="At Home">Home</a></li>
<li><a href="storywork.php" title="At Work">Work</a></li>
<li><a href="storyfamily.php" title="With Family">Family</a></li>
<li><a href="storyfriends.php" title="With Friends">Friends</a></li>
<li><a href="storystrangers.php" title="With Strangers">Strangers</a></li>
<li><a href="storytransport.php" title="In Transport">Transport</a></li>
</ul>
</div>
<div id="addcontainer">
<?
$con = mysql_connect("localhost","root","Pword");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sas_stories", $con);
$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$story = $_POST['story'];
$title = $_POST['category'];
$sql = null;

if ($title == 'home') {
$sql = "INSERT INTO 'sas_stories'.'home_stories' ('first', 'last', 'email', 'story', 'id', 'dateadded') VALUES ('$first','$last','$email','$story', NULL, CURRENT_TIMESTAMP);";
}
if ($title == 'work') {
$sql = "INSERT INTO 'sas_stories'.'work_stories' ('first', 'last', 'email', 'story', 'id', 'dateadded') VALUES ('$first','$last','$email','$story', NULL, CURRENT_TIMESTAMP);";
}
if ($title == 'family') {
$sql = "INSERT INTO 'sas_stories'.'family_stories' ('first', 'last', 'email', 'story', 'id', 'dateadded') VALUES ('$first','$last','$email','$story', NULL, CURRENT_TIMESTAMP);";
}
if ($title == 'friends') {
$sql = "INSERT INTO 'sas_stories'.'friends_stories' ('first', 'last', 'email', 'story', 'id', 'dateadded') VALUES ('$first','$last','$email','$story', NULL, CURRENT_TIMESTAMP);";
}
if ($title == 'strangers') {
$sql = "INSERT INTO 'sas_stories'.'strangers_stories' ('first', 'last', 'email', 'story', 'id', 'dateadded') VALUES ('$first','$last','$email','$story', NULL, CURRENT_TIMESTAMP);";
}
if ($title == 'transport') {
$sql = "INSERT INTO 'sas_stories'.'transport_stories' ('first', 'last', 'email', 'story', 'id', 'dateadded') VALUES ('$first','$last','$email','$story', NULL, CURRENT_TIMESTAMP);";
}

if ($sql) {
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
} echo'<h2 class="centered">Thank you for adding your story !</h2>';
echo '<br /><h4><a href="index.php" title="Return to the previous page or choose a category below." style="color:#EEEEEE;">&laquo; Go back or choose a category above.</a></h4>';
}
?>
</div>
</div>
</div>
</body>
</html>
</body>
</html>


Is there any reason why this code wouldn't work? It used as I have used it before. Any and all (suitable) answers welcome :)

p.s - is there any way of tidying this code? It looks a bit messy to me :roll:

thanks :)
Adam C.

davidj
02-20-2010, 09:51 PM
<?php

/////////////////////////////////////
// this should be included ////////

$con = mysql_connect("localhost","root","Pword");

mysql_select_db("sas_stories", $con);
/////////////////////////////////////

$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$story = $_POST['story'];
$title = $_POST['category'];
$sql = null;

function dbinsert($table,$con){

$sql = "INSERT INTO $table (first, last, email, story, id, dateadded) VALUES ('$first','$last','$email','$story', NULL, CURRENT_TIMESTAMP);";

mysql_query($sql,$con);

}

if(isset($_POST['email']) && isset($_POST['story']) && isset($_POST['category'])){

switch($title){

case 'home': dbinsert('home_stories',$con); break;
case 'work': dbinsert('work_stories',$con); break;
case 'family': dbinsert('family_stories',$con); break;
case 'friends': dbinsert('friends_stories',$con); break;
case 'strangers': dbinsert('strangers_stories',$con); break;
case 'transport': dbinsert('transport_stories',$con); break;

}

$message = "Thank you for adding your story !";

}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SAS | Home</title>
<link href="css/main.css" type="text/css" media="screen" rel="stylesheet" />
</head>
<body>
<div id="container_outer">
<div id="container_inner">
<div id="header"><a href="index.php"><img src="images/nav_bar/logo.png" title="Home Page" border="0" /></a></div>
<div id="category">
<ul>
<li><a href="add-comment.php" title="Add A Comment">Add Story</a></li>
<li><a href="storyhome.php" title="At Home">Home</a></li>
<li><a href="storywork.php" title="At Work">Work</a></li>
<li><a href="storyfamily.php" title="With Family">Family</a></li>
<li><a href="storyfriends.php" title="With Friends">Friends</a></li>
<li><a href="storystrangers.php" title="With Strangers">Strangers</a></li>
<li><a href="storytransport.php" title="In Transport">Transport</a></li>
</ul>
</div>
<div id="addcontainer">

<?php echo $message; ?>

</div>
</div>
</div>
</body>
</html>

domedia
02-20-2010, 09:51 PM
Can you describe a little more in detail how it is not working, ie what is not working with it.
Do you see any errors messages?

Adam_C
02-20-2010, 09:57 PM
im not getting any error messages which is confusing :/

the information simply doesnt post to the database...

ive checked over my form too:

<form action="post.php" method="post" name="update" style="height:280px; color:#333333; top:auto;" onsubmit="MM_validateForm('first','','R','last','','R','emai l','','RisEmail','story','','R');return document.MM_returnValue">
<br />
<div id="box">Story: <br />
<textarea name="story" style="width:545px; height:104px; font:'Segoe UI'"></textarea>
</div>
<div id="line"> First Name:
<input type="text" name="first" style="color:#666666;" align="middle" />
<img src="images/spacer.png" height="1" width="1" /> Last Name:
<input type="text" name="last" style="color:#666666;" align="middle" />
<div id="submitbutton">
<input type="image" src="images/go.png" name="submit" align="middle" />
</div>
<br />
Email Address:
<input name="email" type="text" style="color:#666666;" align="middle" />
<img src="images/spacer.png" height="1" width="1" /> Category:
<select name="category" title="category" style="color:#666666; width:150px;" align="middle" >
<option value="home">Home</option>
<option value="work">Work</option>
<option value="family">Family</option>
<option value="friends">Friends</option>
<option value="strangers">Strangers</option>
<option value="transport">Transport</option>
</select>
<img src="images/spacer.png" height="1" width="120" /> </div>
</form>

davidj
02-20-2010, 10:03 PM
you say it just stopped working

something has to of changed for that to happen

I dont like the look of your SQL table references...

'sas_stories'.'home_stories'

unless your joining tables i would omit the table declaration

Adam_C
02-20-2010, 10:07 PM
i used to only have just the table names, but it wouldnt work, so i input data using the phpmyadmin and used the php code it generated in the post.php

it still didnt work, forgot to change it back :)

domedia
02-20-2010, 10:09 PM
Do you receive the thankyou message in the browser after submitting the form?

davidj
02-20-2010, 10:13 PM
tidied version above

Adam_C
02-20-2010, 10:13 PM
still didnt work after changing them back :confused:

Adam_C
02-20-2010, 10:15 PM
thanks for tidied version :)

one problem: i get error:
Warning: mysql_query() expects parameter 2 to be resource, null given in line 22

davidj
02-20-2010, 10:17 PM
i have removed the quotes from the field definitions in your INSERT statement

my version

Adam_C
02-20-2010, 10:19 PM
Do you receive the thankyou message in the browser after submitting the form?

it says 'Thank you for adding your story !' in the site if thats wat u mean :) but still not adding data to db...

davidj
02-20-2010, 10:21 PM
my version will if you use that

Adam_C
02-20-2010, 10:29 PM
i have removed the quotes from the field definitions in your INSERT statement

my version

whats a field definition? sorry should have sed i was amatuer, can you be more specific? :)

davidj
02-20-2010, 10:30 PM
where you declare the field names in the update statement

Adam_C
02-20-2010, 10:36 PM
where you declare the field names in the update statement

do you mean change it so its:
$con = mysql_connect(localhost,root,Pword);

Adam_C
02-20-2010, 10:52 PM
where you declare the field names in the update statement

im not sure what you mean? :confused:

davidj
02-20-2010, 11:38 PM
sorry

I meant INSERT statement

Adam_C
02-20-2010, 11:42 PM
$sql = "INSERT INTO $table (first, last, email, story, id, dateadded) VALUES ('$first','$last','$email','$story', NULL, CURRENT_TIMESTAMP);";

or this:

$sql = INSERT INTO $table ('first', 'last', 'email', 'story', 'id', 'dateadded') VALUES ('$first','$last','$email','$story', NULL, CURRENT_TIMESTAMP);

sorry if i dont understand :confused:

davidj
02-21-2010, 08:49 AM
your first example is correct

Adam_C
02-22-2010, 12:13 AM
your first example is correct

i still get the same error:

Warning: mysql_query() expects parameter 2 to be resource, null given in post.php on line 22

davidj
02-22-2010, 05:34 AM
do you understand PHP?

post your code

Adam_C
02-22-2010, 03:11 PM
do you understand PHP?

post your code

not fully, i only know bits of it which is why i came here.

worked it out now and it works fine though, i think its something to do with the database though, but as long as it works :)

thanks for all your help guys ! 8-)

davidj
02-22-2010, 05:37 PM
wana learn?

Adam_C
02-22-2010, 05:40 PM
no thanks :)

realised a little while ago its only adding the id and the dateadded values, this tells me that somehow the variables form my form aren't being used by the php code?

any suggestions? thanks again :P

Adam_C
02-22-2010, 09:30 PM
any ideas at all?

Adam_C
02-23-2010, 05:24 PM
ive done it now for definite by making a very basic version and implementing it.

thanks for all your help guys !! :D

davidj
02-24-2010, 10:41 AM
excellent!

Always nice when you work it out yourself. Thats when you start to understand it all