Jump to content

PHP Experimentation


knystrom18

Recommended Posts

I've got this so far:

<?php	$con = mysql_connect("localhost","root","w3schools");	if (!$con) {		die('Could not connect: ' . mysql_error());	}		mysql_select_db("jc2", $con);		$sql="INSERT INTO locations (xcoord, ycoord, thing, extra)	VALUES	('$_POST[xcoord]','$_POST[ycoord]','$_POST[thing]','$_POST[extra]'";		if (!($sql)){		die('Error: ' . mysql_error());	}		echo "1 record has been added.";		mysql_close($con);?>

... and I'd like for two things to happen:1. Although on another page, if fill out form info, hit submit, and this script returns "1 record has been added." there is no reflection of that added record where in both phpMyAdmin, or another page where I pull info from MySQL instead of Insert it. How can I fix this/what is wrong with this code?2. After clicking the submit button, my browser (FX 3.6) loads a new page and displays "1 record has been added." Can I display this on the page I submit the info from? How? Javascript?I'll supply more code if needed.Thanks!P.S. Hello again! It's been a while...

Link to comment
Share on other sites

you need to use http://php.net/function.mysql_query..

$result=mysql_query($qry,$con);

which will give you the resultset on success...false on failureyou need to check the resultset

if($result){//query ok}else{//query false}

according to your code $sql is always true..so it will goes always in else part..to proccess the query you need to call mysql_query()2) it is possible with ajax..you need to send the data thorugh ajax to a page..and thaht page will proccess the requst ..and you have to catch it the response..

Link to comment
Share on other sites

1) give this a read. http://www.w3schools.com/php/php_mysql_intro.asp2) can be done without AJAX, just submit the form to the same page the form is on, and put all the conditional handling at the top of the page.

Link to comment
Share on other sites

2) can be done without AJAX, just submit the form to the same page the form is on, and put all the conditional handling at the top of the page.
I still prefer AJAX :)And AJAX is not that hard, with HTML DOM or XML DOM it can create wonderful web interfaces :)And the person might be confused about what you said about conditional handling, let me give an example:
<?phpif (isset($_POST["blabla"]) or isset($_POST["blablabla"])...){	 // Process the form here}else{	 // Echo (or print) the form here}?>

Link to comment
Share on other sites

I still prefer AJAX :)And AJAX is not that hard, with HTML DOM or XML DOM it can create wonderful web interfaces :)And the person might be confused about what you said about conditional handling, let me give an example:
<?phpif (isset($_POST["blabla"]) or isset($_POST["blablabla"])...){	 // Process the form here}else{	 // Echo (or print) the form here}?>

I have nothing against AJAX, but the common convention for processing and displaying form feedback is by submitting to the same page the form is on, because then you can make use of the form tag, and it's semantic functionality. Javascript can still be used to give instantaneous feedback if the form isn't filled out correctly, and the page with the form on it can also display a success message upon successful submition, but yeah, you could do it with AJAX too, although you would have to parse the responseText, which might be more trouble than it's worth if the only reason to use it is to have the effect not refreshing the page.another option to using isset is to check for empty.
Link to comment
Share on other sites

Alright, I've got everything in place except the re-direct.There's only one problem... and I think it's my host, 1&1.When "insert.php", seen below, attempts to add a record to the database, I get this error:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
1&1 states:
You can only access each one exclusively through your server. Direct access to your MySQL databases using a home PC (external ODBC connection) cannot be established.
So... is me putting in data thru my browser on a page I developed, and not thru 1&1's interface considered an "external ODBC connection", whatever that is?insert.php:
<?php	echo"Processing... <br /><br />";		$link = mysql_connect("xxx","xxx","xxx");	if (!$link){		die('Could not connect: ' . mysql_error());	}		$db=mysql_select_db("xxx", $link);	if (!$db){		die('Could not select database: ' . mysql_error());	}		$qry="	INSERT INTO locations (xcoord, ycoord, thing, extra)	VALUES ('$_POST[xcoord]','$_POST[ycoord]','$_POST[thing]','$_POST[extra]')	";		//$result=mysql_query($qry);		if (!mysql_query($qry, $link)){		die('Error: ' . mysql_error());	}		//mysql_query ($sql, $con);	//if (!$sql){	//	die('Error in query: ' . mysql_error());	//}		//If record is successfully added refresh page, else, display the error.		mysql_close($link);?>

Link to comment
Share on other sites

I propose that they mean you should not try to establish a website on a PC which draws data from a database which is on another PC.And, you added // tags everywhere in your code. Why is that? Remove them and try again.And, in this case, this script would be more useful:

<?phpif (isset($_POST["xcoord"]) and isset($_POST["ycoord"]) and isset($_POST["thing"]) and isset($_POST["extra"])){	echo "Processing...";		$con=mysql_connect($server,$user,$pass);	mysql_select_db($db,$con) or die("Could not connect to the database: " . mysql_error());		$xc=mysql_real_escape_string($_POST["xcoord"]);	$yc=mysql_real_escape_string($_POST["ycoord"]);	$th=mysql_real_escape_string($_POST["thing"]);	$ex=mysql_real_escape_string($_POST["extra"]);		$q1=sprintf("INSERT INTO locations (xcoord,ycoord,thing,extra) VALUES ('%s','%s','%s','%s')",$xc,$yc,$th,$ex);		mysql_query($q1) or die("Unable to execute query: " . mysql_error());		echo "1 record has been added.";}else{	echo ""; // Echo your form here, and change the POST variable names accordingly}?>

Hope I could help.Edit: You can check that if the variable is empty or not by:

if (empty($var))orif (!empty($var))

Link to comment
Share on other sites

I think you mean to use && instead of and?to the OP, have you tried installing a local server on your machine using an AMP stack? This provides you the benefit of having a local database to test and allows you to run PHP code on your computer. It will establish a connection to localhost (127.0.0.1) for your database which would also translate to your live database address, so you wouldn't have to worry about switching anything over when going from local to live.

Link to comment
Share on other sites

I think you mean to use && instead of and?to the OP, have you tried installing a local server on your machine using an AMP stack? This provides you the benefit of having a local database to test and allows you to run PHP code on your computer. It will establish a connection to localhost (127.0.0.1) for your database which would also translate to your live database address, so you wouldn't have to worry about switching anything over when going from local to live.
No, I meant to use "and", since it works the same with && sign, if you do not know.I agree with you on the another topic, OP should do that :)
Link to comment
Share on other sites

No, I meant to use "and", since it works the same with && sign, if you do not know.
It isn't exactly the same. Look at the manual page:
// "&&" has a greater precedence than "and"// The result of the expression (true && false) is assigned to $g// Acts like: ($g = (true && false))$g = true && false;// The constant true is assigned to $h and then false is ignored// Acts like: (($h = true) and false)$h = true and false;
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...