Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Help PLEASE

Posted on 2011-03-01
9
Medium Priority
?
262 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 84

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:Aaron Feledy
ID: 35010713
Line 61:
header( "Location: http://www.google.com" );


That code automatically redirects you to google.
0
 
LVL 13

Expert Comment

by:Aaron Feledy
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
Independent Software Vendors: 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 2000 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 111

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

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!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

609 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