Link to home
Start Free TrialLog in
Avatar of wantabe2
wantabe2Flag for United States of America

asked on

PHP Help PLEASE

I've worked with this code all morning & it was working earlier but I've screwed it up somehow! This code is supposed to display a form in a browser, the user inserts the data then submits it & it gets written to a mysql database. At this time, EVERYTIME I go to the htttp://mysite/add.php it goes straight to google & I do not see my form to insert the text in the fields anymore...please help!
<html>

<head>
<title> Update Database </title>
</head>

<?php
#	Page: add.php
##########################

#	Report all errors
##########################
ini_set('display_errors',1);
error_reporting(E_ALL);

#	db connection settings
##########################

#	server IP
$host	= "localhost";
#	sql user
$user	= "root";
#	sql pw
$pw	= "12ibtlalc25";
#	sql database
$db	= "psrflow";

#	get submitted values & escape the text string
#################################################
if ( isset( $_POST['offender_fname'] ) ){
	$offender_fname = addslashes( $_POST['offender_fname'] );
}
if ( isset( $_POST['offender_lname'] ) ){
	$offender_lname = addslashes( $_POST['offender_lname'] );
}
if ( isset( $_POST['location'] ) ){
	$location = addslashes( $_POST['location'] );
}

#	connect to db server
##########################
$conn = mysql_connect( $host, $user, $pw )
or die( "Error! Unable to connect to database server: <br/>" . mysql_error() );
#	connect to db
##########################
$rs = mysql_select_db( $db, $conn )
or die( "Error! Unable to connect to database:  <br/>" . mysql_error() );


$strSQL = "INSERT INTO psrinfo
	( offender_fname, offender_lname, location )
	VALUES
	( '" . $offender_fname . "', '" . $offender_lname . "', '" . $location . "' )";

#	execute db insert
##########################
if (!mysql_query( $strSQL, $conn )){
	echo( "Unable to save data to database: <br/>" . mysql_error() . "<br/>" . $strSQL . "</span><br/>" );
}
else{
	header( "Location: http://www.google.com" );
}
?>






<body>
<form method="post" action="add.php">

First Name: <br />

<input type="text" name="offender_fname" size="30" /><br />

Last Name: <br />

<input type="text" name="offender_lname" size="30" /><br />

Location: <br />

<input type="text" name="location" size="30" /><br />


<input type="submit" value="Submit" />


</form>
</body>
</html>

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Get rid of the redirect to Google.  You have it set up so that if the PHP works properly, it goes to Google.  I don't think that's what you want.
Line 61:
header( "Location: http://www.google.com" );


That code automatically redirects you to google.
Also, be sure that is not your actual username and password for your db in that code... if so, you just posted that for all to see :-/
Avatar of wantabe2

ASKER

It's not the real UN & PW but thanks for checking that. I still can't get it to work, if I take the re-direct out I get a page can not be displayed & blank entry gets put in the mysql database.
I've narrowed it down to this simple code & it still will not insert the data into the mysql database.....what am I missing...I am very new to programming as you can see...
<html>

<?php
	$config["HOSTNAME"]		= "localhost";
	$config["USERNAME"]		= "root";
	$config["PASSWORD"] 	= "12ibtlalc25";
	$config["DBNAME"]      	= "psrflow";
	
	$dbconn = mysql_pconnect($config["HOSTNAME"], $config["USERNAME"], $config["PASSWORD"]) or  die(mysql_error());
	mysql_select_db($config["DBNAME"] , $dbconn);
?>

<head>
<title> Update Database </title>
</head>

<body>
<form method="post" action="" onSubmit="return checkme()">

 <b>First Name:</b> <br />

<input type="text" name="offender_fname" size="35" /><br />

<b>Last Name:</b> <br />

<input type="text" name="offender_lname" size="35" /><br />

<b>Location:</b> <br />

<input type="text" name="location" size="35" /><br />


<input type="submit" value="Submit" />

</form>
</body>
</html>

Open in new window

Where you have this

$strSQL = "INSERT INTO psrinfo
	( offender_fname, offender_lname, location )
	VALUES
	( '" . $offender_fname . "', '" . $offender_lname . "', '" . $location . "' )";

#	execute db insert
##########################
if (!mysql_query( $strSQL, $conn )){
	echo( "Unable to save data to database: <br/>" . mysql_error() . "<br/>" . $strSQL . "</span><br/>" );
}
else{
	header( "Location: http://www.google.com" );
}
?>

Open in new window

You are ALWAYS executing the query. What you need to do is ONLY execute it on someone pressing the submit button. Start by giving your submit button a name

<input name='submit' type="submit" value="Submit" />

Then detect it in the code

if ( isset( $_POST['submit'] == "Submit" ) {
......code here
}

then insert your SQL statement where I wrote .....code here in the fragment above


$strSQL = "INSERT INTO psrinfo
	( offender_fname, offender_lname, location )
	VALUES
	( '" . $offender_fname . "', '" . $offender_lname . "', '" . $location . "' )";

if ( isset( $_POST['submit'] == "Submit" ) {
   #	execute db insert
   ##########################
   if (!mysql_query( $strSQL, $conn )){
   	   echo( "Unable to save data to database: <br/>" . mysql_error() . "<br/>" . $strSQL . "</span><br/>" );
   }
}

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PERFECT! Thanks
Get rid of addslashes().  See the note here about that, "It's highly recommended to use DBMS specific escape function (e.g. mysqli_real_escape_string() for MySQL..."

I think hielo has got you on the right track.  Going forward, it is usually easiest to write and debug your code in tiny bits.  First make sure you can get the form input and print it out (learn about http://us2.php.net/manual/en/function.var-dump.php), then make sure you can write the data into the data base.  When each part of the app works, add the next layer.

You might also want to learn about version control with GitHub or SubVersion.  Then you won't lose your old copies of code that worked!