Jump to content

help with php --> database


gibigbig

Recommended Posts

ok the expected results are that when i go into index.php, i am supposed to see 2 boxes. whatever i type in both boxes should come up in the database under the fields "username" and "message" but for some reason, only "message" field gets filled when i chack in php my admin. here are my codes, canu tell me what i did wrong?

mysql_connect.php

<!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>Untitled Document</title>

<style type="text/css">
<!--
.connected {color:#FFFFFF;
font-family:Arial, Helvetica, sans-serif;
font-size:10px;
background-color:#000000;
padding:5px;
border:thin #999999;
}
-->
</style>
</head>

<body>
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
echo "<div class='connected'>Connected to MySQL<br />Connected to Database</div>";
?></body></html>

 

index.php

<!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>Untitled Document</title>
</head>
<body>
<form action="process.php" method="post">
  <p>
    <input name="username" type="text" />
<p>
    <textarea name="message" cols="50" rows="10"></textarea><br /><input value="submit message" type="submit" />
  </p>
</form>

</html>

 

 

process.php

<!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>Untitled Document</title>
</head>

<body><?php include('mysql_connect.php'); ?>
<?php 
$username = $_POST['username'];
$message = $_POST['message'];
mysql_query("INSERT INTO Users (username,message) VALUES ('$item','$message')");
echo"message added"; ?>
</body>
</html>

Link to comment
Share on other sites

I compressed all of your files into one to make it easier and i added in error checking. i found that you were inserting $item into the db for username instead of $username but i added in the error checking to show you if anything breaks

 

<html>
<head>
	<title>Inbox</title>
</head>
<body>
	<?php 
		if(mysql_connect("localhost", "username", "password")) {
			if(mysql_select_db("database")) {
				if(isset($_POST['submit'])) {
					if(isset($_POST['username']) && isset($_POST['message'])) {
						$username = mysql_real_escape_string($_POST['username']);
						$message = mysql_real_escape_string($_POST['message']);
						if(mysql_query("INSERT INTO Users (username,message) VALUES ('" . $username . "','" . $message . "')"));
							$noerror = 'Message Sent'; 
						}
						else {
							$error = 'Cannot connect to database. Please notify an admin with this error: ' . mysql_error();
						}
					}
					else {
						$error = 'You must submit all of the information';
					}
				}
				else {
					print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>';
					print '<table>';
					print '<tr><td width=100px> User Name: </td><td> <input name=username type=text /> </td></tr>';
					print '<tr><td width=100px> Message: </td><td> <textarea name=message cols="50" rows="10"></textarea> </td></tr>';
					print '</table>';
					print '<input type=submit name=submit value="Send Message" />';
					print '</form>';
				}
			}
			else {
				$error = 'Cannot connect to database. Please notify an admin with this error: ' . mysql_error();
			}
		}
		else {
			$error = 'Cannot connect to database. Please notify an admin with this error: ' . mysql_error();
		}
	?>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.