Solved

PHP Help PLEASE

Posted on 2011-03-01
9
244 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:wantabe2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35010707
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.
0
 
LVL 13

Expert Comment

by:Arrow_1
ID: 35010713
Line 61:
header( "Location: http://www.google.com" );


That code automatically redirects you to google.
0
 
LVL 13

Expert Comment

by:Arrow_1
ID: 35010727
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 :-/
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Author Comment

by:wantabe2
ID: 35010755
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.
0
 
LVL 15

Author Comment

by:wantabe2
ID: 35010840
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

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35010872
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


0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 35010922
Try:
<?php
#	Page: add.php
##########################

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

if( isset($_POST) && !empty($_POST) )
{
     #	db connection settings
     ##########################
     
     #	server IP
     $host	= "localhost";
     #	sql user
     $user	= "root";
     #	sql pw
     $pw	= "12ibtlalc25";
     #	sql database
     $db	= "psrflow";
     
     
     #	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() );
     
     
     #	get submitted values & escape the text string
	foreach($_POST as $key=>$value)
	{
		${$key}=mysql_real_escape_string($value);
	}

     $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" );
		exit;
     }
}
?>
<html>

<head>
<title> Update Database </title>
</head>
<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

0
 
LVL 15

Author Closing Comment

by:wantabe2
ID: 35011376
PERFECT! Thanks
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35011417
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!
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generate PDF's using TCPDF - Initial Set Up 9 34
Special characters in a TCPDF 4 28
Calculating percentage 2 32
How to get chosen background-color on every line? 10 21
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question