+ Reply to Thread
Results 1 to 6 of 6

Thread: MYSQL/PHP to Simple Form

  1. #1
    e85andyou is offline x10Hosting Member e85andyou is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    49

    MYSQL/PHP to Simple Form

    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 by e85andyou; 10-23-2008 at 08:20 AM.

  2. #2
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: MYSQL/PHP to Simple Form

    PHP Code:
    <?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 by xmakina; 10-23-2008 at 09:58 AM. Reason: The error function was from my other things. Oops :S
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

  3. #3
    e85andyou is offline x10Hosting Member e85andyou is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    49

    Re: MYSQL/PHP to Simple Form

    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 by e85andyou; 10-23-2008 at 09:21 AM.

  4. #4
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: MYSQL/PHP to Simple Form

    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 by xmakina; 10-23-2008 at 09:29 AM.
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

  5. #5
    e85andyou is offline x10Hosting Member e85andyou is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    49

    Re: MYSQL/PHP to Simple Form

    Thanks for all the help xmakina code is working well


    You can close this thread

  6. #6
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: MYSQL/PHP to Simple Form

    My pleasure
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

+ Reply to Thread

Similar Threads

  1. [PHP] How to create a simple contact form
    By nightscream in forum Tutorials
    Replies: 6
    Last Post: 01-23-2011, 11:37 AM
  2. Addon Domain PHP Form Error
    By rockee in forum Free Hosting
    Replies: 2
    Last Post: 04-02-2008, 06:53 PM
  3. Two very simple php file upload scripts-Free
    By Symbian.Ankit in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 02-20-2008, 09:55 AM
  4. Design a free form
    By gateway in forum Off Topic
    Replies: 1
    Last Post: 10-19-2007, 05:49 PM
  5. A simple Visual Basic Login Form
    By Zenax in forum Tutorials
    Replies: 0
    Last Post: 03-13-2007, 08:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers