MYSQL/PHP to Simple Form

e85andyou

New Member
Messages
49
Reaction score
0
Points
0
I constantly add store locations to my database but the way I do so is driving me nuts. The code I have pulls the addresses from a text document. I would like to use a simple form to submit the information to the database but I have trouble with database programming. I will list my current code below for information. Since nothing in this world comes free the first person to help out with a working form receives 300 credits.

It does not matter what language you use as long as the job gets done.


Current Code
Code:
$db_server = "localhost";
$db_name = "DBName";
$db_user = "UserName";
$db_pass = "password";
$con = mysql_connect($db_server, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_name, $con);

   $file_name = "newstations.txt";
   $str = file_get_contents($file_name);
   $str = str_replace("'", "@", $str);
   $stat_arr = explode("\n\n\n", $str);
   $stat_count = count($stat_arr);
   for ($i = 0; $i < $stat_count; $i++) { $temp_arr = explode("\n", $stat_arr[$i]);
   $loc_arr = explode(" ", $temp_arr[2]); 
   
   if (count($loc_arr) == 3)     {         $city = trim($loc_arr[0], ",");         $state = $loc_arr[1];         $zip = $loc_arr[2];     }     else if (count($loc_arr) == 4)     {         $city = $loc_arr[0] . " " . trim($loc_arr[1], ",");         $state = $loc_arr[2];         $zip = $loc_arr[3];     }     mysql_query("INSERT INTO station (name, address, city, state, zip, phone) VALUES ('$temp_arr[0]', '$temp_arr[1]', '$city', '$state', '$zip', '$temp_arr[3]')") or die(mysql_error()); } ?>
Simple Code to speed things up:
Code:
<form action="" method="" >

Name
<input type="text" name="name" value="" id="name"  /><br />
Address
<input type="text" name="address" value="" id="address"  /><br />
City
<input type="text" name="city" value="" id="city"  /><br />
State
<input type="text" name="state" value="" id="state"  /><br />
Zip
<input type="text" name="zip" value="" id="zip"  /><br />
Phone
<input type="text" name="phone" value="" id="phone"  /><br />

<input type="submit" value="Add Station" />

</form>
 
Last edited:

xmakina

New Member
Messages
264
Reaction score
0
Points
0
PHP:
<?php
	function addStore($name, $address, $city, $state, $zip, $phone){
		$db_server = "localhost";
		$db_name = "DBName"; //CHANGE THIS
		$db_user = "UserName"; //CHANGE THIS
		$db_pass = "password"; //CHANGE THIS
		$con = mysql_connect($db_server, $db_user, $db_pass) or die(mysql_error());
		mysql_select_db($db_name, $con);
		
		$sql = "INSERT INTO station (name, address, city, state, zip, phone)
				VALUES ('".$name."', '".$address."', '".$city."', '".$state."', '".$zip."', '".$phone."')";
		$result = mysql_query($sql);
		if(!$result){
			return ("<p>SQL Error<br />".$sql."<br />".mysql_error()."</p>");
		}
		return ("<p>Store added!</p>");
	}
?>

<form action="<FILENAME>" method="POST" >
	<input type="hidden" name="act" value="add" />
	Name
	<input type="text" name="name" value="" id="name"  /><br />
	Address
	<input type="text" name="address" value="" id="address"  /><br />
	City
	<input type="text" name="city" value="" id="city"  /><br />
	State
	<input type="text" name="state" value="" id="state"  /><br />
	Zip
	<input type="text" name="zip" value="" id="zip"  /><br />
	Phone
	<input type="text" name="phone" value="" id="phone"  /><br />

	<input type="submit" value="Add Station" />
</form>

<?php
	if($_POST['act'] == "add"){
		echo addStore($_POST['name'], $_POST['address'], $_POST['city'], $_POST['state'], $_POST['zip'], $_POST['phone']);
	}
?>

Enjoy :)

EDIT;

If you do encounter errors, the script will dump the SQL for you. Pop that into phpMyAdmin's SQL tool and it should give you a good idea where the problem is. If you don't understand the error message, post it here and I'll fix it :)
 
Last edited:

e85andyou

New Member
Messages
49
Reaction score
0
Points
0
I ran the coding through PHPmyAdmin and received the following information. I have already handed out the 300 credits but im giving 20 credits for the solution of this error

ERROR: Unknown Punctuation String @ 1
STR: <?
SQL: <?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";<?php
function addStore($name, $address, $city, $state, $zip, $phone){
$db_server = "localhost";


SQL query:

<?php function addStore($name, $address, $city, $state, $zip, $phone){ $db_server = "localhost";

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<?php
function addStore($name, $address, $city, $state, $zip, $phone){
' at line 1
 
Last edited:

xmakina

New Member
Messages
264
Reaction score
0
Points
0
Er... You missed what I said there.

I meant if you run the script on your WEBSITE and the SQL is invalid it will echo the contents of the variable $sql which is what you put into phpMyAdmin

So if something is wrong your Website will say

SQL Error
<SQL STATEMENT>
<SQL ERROR DESCRIPTION>

Put <SQL STATEMENT> into phpMyAdmin. Hope that's made it a little clearer.
 
Last edited:

e85andyou

New Member
Messages
49
Reaction score
0
Points
0
Thanks for all the help xmakina code is working well


You can close this thread
 
Top