Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question php script w/ "if..else" upload to mysql database help?

    ok so im def a noob at this, i made my site w/ dreamweaver, and also using sublime 2 to code. My main source of info has been w3school. any way after realizing that the form was useless w/ out a php script, i proceeded to write that, of course the same sources. So ive got the php script containing "if...else" . My html form has 5 (inputs)? with one of them being a radio input w/ 3 buttons. So i have the managed to build the correct database on my host site with phpmyadmin (it doesnt allow local) and im up to the part where it wont let me upload the php script due to error 1064 or some thing. so i noticed another method and this is where i am stuck. in themysql query, i get the "SELECT * FROM `Form` WHERE 1"... I am unable to find the correct method to do this using "if...else" Does any one have any advice? my php script is as follows....either i did something wrong or i need a specfic way to upload it? of course if im mistaken plz tell me...thank you guys for your time and inputl.

    PHP Code:
    <?php
    // define variables and set to empty values
    $nameErr $emailErr $phoneErr $contactErr $messageErr "";
    $name $email $phone $contact $message "";
     
     if (
    $_SERVER["REQUEST_METHOD"] == "POST")   {
         if (empty(
    $_POST["name"]))  {
             
    $nameErr "Name Is Required";
         } else {
             
    $name test_input($_POST["name"]);
             
    // check if name only contains letters and white space
             
    if (!preg_match("/^[a-zA-Z]*$/",$name))   {
                 
    $nameErr "Please Enter Valid Name";
             }
         }
         
          if (empty(
    $_POST["email"]))  {
             
    $emailErr "Email Is Required";
         } else {
             
    $email test_input($_POST["email"]);
             
    // check is e-mail address syntax is valid
             
    if (!preg_match("?([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))      {
                 
    $emailErr "Email Is Invalid Format";
             }
             
         }
         
         if (empty(
    $_POST["phone"]))  {
             
    $phone "";
         } else {
             
    $phone test_input($_POST["phone"]);
             
    //check if phone only contains numbers
             
    if (!preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i",$phone))   {
             
    $phoneErr "Please Enter A Valid Phone Number";
             }
         }
         if (empty(
    $_POST["contact"]))  {
             
    $contactErr "Please Choose An Option";
         } else {
             
    $contact test_input($_POST["contact"]);
         }
          if (empty(
    $_POST["message"]))  {
             
    $messageErr "Please Enter A Brief Description";
         } else {
             
    $message test_input($_POST["message"]);
                 }

     }
     
    ?>
    Last edited by FishMonger; 05-03-2014 at 08:15 PM. Reason: Added code tags

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,719
    Thanks
    25
    Thanked 662 Times in 661 Posts
    error 1064 has nothing to do with uploading a php script, it means your query has errors. The PHP file you quoted here has nothing to do with querying a database. It is checking the validation of the info that was sent, but does not send the errors back to the user.
    Also, I see nothing here for evaluating the 3 radio buttons.
    Please give us the HTML code your using so we can look at your form.
    And maybe the old PHP script.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here's the html script. def hit a wall on this thanks for the help. i know im missing something here, but i cant figure it out.
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Contact US</title>
    <div align="right">Follow US on <br><!(LINKs HERE)></div>
    </head>

    <body background="../background.jpg">
    <table width="750" border="1" align="center">
    <tr>
    <td><img src="../MASSWORLD.US.png" width="1012" height="200" alt=""/></td>
    </tr>
    </table>
    <div align="left">
    <table width="1056" border="1" align="center">
    <tr align="center">
    <td width="150" bgcolor="#65AC84"><div align="center"><a href="http://massworld.us/" style="color: #000000">US </a></div></td>
    <td width="150"><div align="center"><a href="http://forsale.massworld.us/" style="color: #000000">Buy From US</a></div></td>
    <td width="150"><div align="center"><a href="http://sell.massworld.us/" style="color: #000000">Sell To US</a></div></td>
    <td width="150"><div align="center"><a href="http://reviews.massworld.us/" style="color: #000000">Review US</a></div></td>
    <td width="150"><div align="center"><a href="http://about.massworld.us/" style="color: #000000">About US</a></div></td>
    <td width="150"><div align="center"><a href="http://contact.massworld.us/" style="color: #000000">Contact US</a></div></td>
    </tr>
    </table>
    </div>
    <table width="1350" height="719" border="1" align="left" id="list">
    <tr>
    <th width="337" height="46" bgcolor="#382F1C" scope="row"><p style="color: #276FF5"><u>Recently listed</u></p></th>
    <th width="676" align="center" nowrap bgcolor="#07C000" scope="row"><div align="center"><u style="color: #F11919"><strong style="font-size: 36px; color: #DF0303;">Contact US</strong></u></div></th>
    <th width="337" height="46" bgcolor="#276FF5" style="font-size: 16; color: #382F1C;" scope="row"><u>Our Friends</u></th>
    </tr>
    <tr>
    <th width="337" scope="col"><div align="center"> <div position="top">
    <h1 style="font-size: 14px;">
    <img src="../laptop.jpg"><br><a href="http://www.ebay.com/itm/271465936959?ssPageName=STRK:MESELX:IT&_trksid=p3984.m1555.l2649/">Toshiba Laptop</a><br>
    <img src="../joystick red.jpg" width="180" height="100" alt=""/><br><a href src="http://www.ebay.com/itm/271465859136?ssPageName=STRK:MESELX:IT&_trksid=p3984.m1555.l2649">Red Joystick Cap For Gaming Controller (Universal)</a>
    <img src="../joystick yellow.jpg" width="180" height="100" alt=""/><br><a href="http://www.ebay.com/itm/271465865960?ssPageName=STRK:MESELX:IT&_trksid=p3984.m1555.l2649">Yellow Joystick Cap For Gaming Controller (Universal)</a>
    </h1>
    <h2 style="font-size: 16px;"><u>SOLD</u><br><img src="../IPhone 4s.jpg" width="180" height="100" alt=""/><br>
    <a href="http://www.ebay.com/itm/271427985105?ssPageName=STRK:MESOX:IT&_trksid=p3984.m1561.l2649"><s>Iphone 4S</s></a><br>
    <img src="../250GB HD.jpg" width="180" height="100" alt=""/><br>
    <a href="http://www.ebay.com/itm/271460708401?ssPageName=STRK:MESOX:IT&_trksid=p3984.m1561.l2649"><s>250GB Hard Drive
    </s></a></h2>
    </div>
    </div>
    </th>
    <th width="656" align="left" valign="top" scope="col">

    <h1>In need of a repair? Interested in Upgrading your device?
    Or just have a general question? Then you're in the right place! Fill out the form bellow and you will recieve a reply within 1-3 business days.</h1>
    <p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">

    First Name*:<br><input name="name" type="text" required>
    <span class="error">* <?php echo $nameErr;?></span><br>
    Email*:<br><input name="email" type="text" required>
    <span class="error">* <?php echo $emailErr;?></span><br>
    Phone #:<br><input type="number" name="phone"><br>
    Check one of the folllowing<br><input type="radio" name="contact"value="repair">Repair<input type="radio" name="contact" value="upgrade">Upgrade<input type="radio" name="contact" value="other">Other<span class="error">* <?php echo $contactErr;?></span><br><br>
    Tell US how we can help:<br><textarea rows="10" cols="30"> </textarea><br><input type="submit" value="Send"><input type="reset" value="Reset"><span class="error">* <?php echo $messageErr;?></span>

    </form>
    </p>



    </th>

    <th width="337" scope="col"><div align="center">(AD SPACE)</div>

    </tr>
    </table>
    <p align="right" style="text-align: left">&nbsp;</p>
    <blockquote>&nbsp;</blockquote>
    <p>&nbsp;</p>
    </body>

    </html>

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,719
    Thanks
    25
    Thanked 662 Times in 661 Posts
    The file you should be using is a combination of the PHP (It's incomplete) and the HTML. The final script should be labeled index.PHP and this is what the final looks like:
    PHP Code:
    <?php
    // define variables and set to empty values
    $nameErr $emailErr $phoneErr $contactErr $messageErr "";
    $name $email $phone $contact $message "";

    if (
    $_SERVER["REQUEST_METHOD"] == "POST"){
        if (empty(
    $_POST["name"])){
            
    $nameErr "Name Is Required";
        } else {
            
    $name test_input($_POST["name"]);
            
    // check if name only contains letters and white space
            
    if (!preg_match("/^[a-zA-Z]*$/",$name)){
            
    $nameErr "Please Enter Valid Name";
            }
        }
        if (empty(
    $_POST["email"])){
            
    $emailErr "Email Is Required";
        } else {
            
    $email test_input($_POST["email"]);
            
    // check is e-mail address syntax is valid
            
    if (!preg_match("?([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))      {
                
    $emailErr "Email Is Invalid Format";
            }
        }
        if (empty(
    $_POST["phone"])){
            
    $phone "";
        } else {
            
    $phone test_input($_POST["phone"]);
            
    //check if phone only contains numbers
            
    if (!preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i",$phone))   {
                
    $phoneErr "Please Enter A Valid Phone Number";
            }
        }
        if (empty(
    $_POST["contact"])){
            
    $contactErr "Please Choose An Option";
        } else{
            
    $contact test_input($_POST["contact"]);
        }
        if (empty(
    $_POST["message"])){
            
    $messageErr "Please Enter A Brief Description";
        } else {
            
    $message test_input($_POST["message"]);
        }
    }
     
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Contact US</title>
    <div align="right">Follow US on <br><!(LINKs HERE)></div>
    </head>

    <body>

    <form method="post" action="">
    First Name*:<br><input name="name" type="text" required>
    <span class="error">* <?php echo $nameErr;?></span><br>

    Email*:<br><input name="email" type="text" required>
    <span class="error">* <?php echo $emailErr;?></span><br>

    Phone #:<br><input type="number" name="phone">
    <span class="error">* <?php echo $emailErr;?></span><br><br>
    Check one of the folllowing<br>
    <input type="radio" name="contact"value="repair">Repair
    <input type="radio" name="contact" value="upgrade">Upgrade
    <input type="radio" name="contact" value="other">Other
    <span class="error">* <?php echo $contactErr;?></span><br><br>


    Tell US how we can help:<br>
    <textarea rows="10" cols="30">
    </textarea><br>
    <input type="submit" value="Send">
    <input type="reset" value="Reset">
    <span class="error">* <?php echo $messageErr;?></span>
    </form>

    </body>
    </html>
    It is the php you gave us and a CUT DOWN version of the HTML to just show the <form>

    In the PHP you have this at every check point
    PHP Code:
    $name test_input($_POST["name"]); 
    But the function test_input() does not exist.
    When your validation proves to have no errors you do not process the form information.

    P.S. I just don't like
    PHP Code:
    action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"
    the action="" or action="#" is my preferred way of sending the form to it's self.

    Not fond of
    PHP Code:
    if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
    but that works and is OK.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •