SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Script working but not inserting into database

    Hi,

    not sure if this is a php question or database question but I cant work it out.

    I have created a form with php script to validate and send the values to a database table called briefin_form.

    The proccess seenms to go right through as it throws up the page to tell the user we will be in contact with them.

    However when I look at the table in the database. No row has been inserted. Can anyone spot the glaring mistake because its driving me scatty.

    The script for the form is as follows:

    PHP Code:
    <?php require_once("includes/connection.php");
    include (
    "includes/functions.php");
    //form validation
    if(isset($_POST['submit'])){
    $errors=array();
    $requiredfields=array('market'=>'Please enter the market question','style'=>'Please answer the style question If not sure just put unsure','message'=>'Please answer the message question If not sure just put unsure','branding'=>'Please answer the branding question If not sure put unsure','name'=>'Please enter your name','email'=>'Please enter your email so we can contact you');
    foreach(
    $requiredfields as $fieldname => $requiredfieldsmessage){
    if(!isset(
    $_POST[$fieldname]) || empty($_POST[$fieldname]) || strlen(trim($_POST[$fieldname])) == 0) {
    $errors[] = $requiredfieldsmessage;
    }
    }
     if(
    count($errors) == 0){
     
     
    $_POST array_map('mysqli_prep'$_POST);
     
    //end form validation
    $primary_function  mysqli_prep($_POST['primary_function']);
    $go_live  mysqli_prep($_POST['go_live']);
    $biz_type  mysqli_prep($_POST['biz_type']);
    $market  mysqli_prep$_POST['market']);
    $style  mysqli_prep$_POST['style']);
    $message  mysqli_prep($_POST['message']);
    $branding  mysqli_prep($_POST['branding']);
    $budget  mysqli_prep($_POST['budget']);
    $images  mysqli_prep($_POST['images']);
    $name  mysqli_prep($_POST['name']);
    $email  mysqli_prep($_POST['email']);
    $query "INSERT into briefing_form (
    primary_function,go_live,biz_type,market,style,message,branding,budget,images,name,email)Values('
    {$primary_function}','{$go_live}','{$biz_type}','{$market}','{$style},'{$message}','{$branding}','{$budget}','{$images}','{$name}','{$email}')";
    $result mysqli_query($connection$query);
    header('Location: brief_return.php');
    exit;
    }
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Welcome to market!</title>
    <link href="Xind4.css" rel="stylesheet" type="text/css" />
      <!--[if lte IE 7]>
        <link rel="stylesheet" type="text/css" href="iehacks2.css" />
      <![endif]-->

    <style type="text/css">
    <!--
    #Layer1 {
        position:absolute;
        width:543px;
        height:115px;
        z-index:6;
        left: 104px;
        top: 159px;
    }
    #Layer2 {
        position:absolute;
        width:200px;
        height:56px;
        z-index:1;
        left: 139px;
        top: 142px;
    }
    #Layer3 {
        position:absolute;
        width:200px;
        height:42px;
        z-index:6;
        left: 453px;
        top: 483px;
    }
    #Layer4 {
        position:absolute;
        width:212px;
        height:43px;
        z-index:7;
        left: 455px;
        top: 727px;
    }
    #Layer5 {
        position:absolute;
        width:128px;
        height:130px;
        z-index:5;
        left: 28px;
        top: 125px;
        outline: #FF0000
    }
    #Layer6 {
        position:absolute;
        width:79px;
        height:96px;
        z-index:1;
        left: 24px;
        top: -1px;
        outline: #FF0000;
    }
    #Layer7 {
        position:absolute;
        width:200px;
        height:43px;
        z-index:8;
        left: 453px;
        top: 983px;
    }
    #Layer8 {
        position:absolute;
        width:139px;
        height:131px;
        z-index:6;
        left: 245px;
        top: 125px;
    }
    #Layer9 {
        position:absolute;
        width:83px;
        height:93px;
        z-index:1;
        left: 22px;
        top: 0px;
    }
    #Layer10 {
        position:absolute;
        width:138px;
        height:115px;
        z-index:7;
        left: 432px;
        top: 130px;
    }
    #Layer11 {
        position:absolute;
        width:86px;
        height:90px;
        z-index:1;
        left: 20px;
        top: 4px;
    }
    #Layer12 {
        position:absolute;
        width:156px;
        height:115px;
        z-index:8;
        left: 401px;
        top: 310px;
    }
    .style1 {color: #666666}
    a:link {
        text-decoration: none;
    }
    a:visited {
        text-decoration: none;
    }
    a:hover {
        text-decoration: none;
    }
    a:active {
        text-decoration: none;
    }


    -->
    </style>
    </head>

    <body >
    <div id="wrapper">
    <div id="topbit">
    <div id="header">
      <img src="myimages/marketsheader2.jpg" alt="All you need to advertise youe web site on the internet" width="708" height="89" border="0" usemap="#Map" />
    <map name="Map" id="Map"><area shape="rect" coords="567,7,684,78" href="http://www.outitgoes.com/" target="_blank" />
    </map></div>
    <!--end header-->
    <div id="mailhead"><a href="http://www.outitgoes.com/"></a><img src="myimages/contact.gif" alt="Log in to your Control Panel" width="131" height="84" border="0" usemap="#Map2" />
    <map name="Map2" id="Map2"><area shape="rect" coords="5,26,121,51" href="contact.php" />
    </map></div>
    <!--end of mailhead--></div><!--end topbit-->

    <div id="mainbody">
    <div id="menu">
    <ul style="width:870px; font:Arial, Helvetica, sans-serif">
    <li style="padding-left:40px;" ><a  style="width:54px; padding-left:2px;" href="index.php">Home</a></li>
    <li style="padding-left:20px;"><a style="width:104px;" href="contact.php">Contact Us</a></li>
    <li style="padding-left:20px;"  ><a style="width:134px;"  href="http://www.the-bee-scene.com/hostpay">Hosting Packages</a></li>
    <li style="padding-right:20px; padding-left:10px;" ><a style="width:104px;" href="design.php">Web Design</a></li>
    <li><a   style="width:94px;"  href="promotion.php">Be Seen!</a></li>
    <li ><a style="width:74px;" href="http://www.eXtendcp.com/">Log In</a></li>
    <li style="padding-left:30px;"><a  style="width:104px;" href="http://www.the-bee-scene.com/hostpay/basket?stay=1">View Basket</a></li>
    </ul>
     </div><!--end menu-->
    <div id="offers"> 
    </div><!--end offers-->
    <div class="offers1">
      <div align="justify"><span style="font-size:12px">Our focus at Markets2000 is not just about getting a web site on the internet but making its stay there efficient, cost effective and business friendly. Marketing your site begins with how it is designed, monitered and promoted on the web. so we offer two options to get new business going. Choose you build and we offer quality hosting packages, your own domain and a bundle of apps to make the build easy, track and monitor your sites progress. Or if you choose WE-BUILD we can design, develop, monitor and market the site at very competetive prices. Simple! Your choice- Take a look.</span></div><!--end offers1-->
    </div>
    <div id="leftside"></div><!--end leftside-->
    <div id="form">
    <?php
    if(count($errors) != 0){ 
    echo 
    '<h3>Sorry, please fill in the following text boxes. If not sure put "unsure"</h3>';
    echo 
    '<ul>'
    foreach (
    $errors as $error) { 
    echo 
    '<li>' $error '</li>';}echo '</ul>';
    }
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table width="400px;" border="1">
    <tr>
    <td><p><b>What will be the primary function of your web site?</b></p>
    <input type="radio" name="primary_function" value "0" id="primary_function" /> A simple Brochure<br/>
    <input type="radio" name="primary_function" value "1" id="primary_function" />A site that allows you to add and change content when you wish?<br/>
    <input type="radio" name="primary_function" value "2" id="primary_function"/>An E-commerce site?<br/>
    <input type="radio" name="primary__function" value "3" id="primary_function"/>A site that will include user membership?<br/></td>
    </tr>
    <tr>
    <td>
    <p><b>When would you like the site to go live?</b></p>
    <input type="radio" name="go_live" value "0" id="go_live"  />One month<br/>
    <input type="radio" name="go_live" value "1" id="go_live"/>Two months<br/>
    <input type="radio" name="go_live" value "2" id="go_live"/>Three months<br/>
    <input type="radio" name="go_live" value "3" id="go_live" />More than three months<br/>
    <!--If you have a specific deadline please state it here:
    <input type="text" name="live" value=""  />--></td>
    </tr>
    <tr>
    <td>
    <p><b>What is your type of business, what are are your products and/or services?</b></p>
    <textarea name="biz_type" cols="40" rows="5" id="biz_type"  > <?php echo htmlspecialchars($_POST['biz_type'],ENT_QUOTES); ?></textarea></td>
    </tr>
    <tr>
    <td>
    <p><b>Who is your market?</b> (i.e. teenagers;male,female; animal lovers; artistic people;  etc)</p>
    <textarea name="market" cols="40" rows="5" id="market"  > <?php echo htmlspecialchars($_POST['market'],ENT_QUOTES); ?></textarea></td>
    </tr>
    <tr>
    <td>
    <p><b>What style are you looking for?</b> (i.e. if there are any web sites you have seen which reflect the style your site should be like please enter the web address.)</p>
    <textarea name="style" cols="40" rows="2"  id="style" > <?php echo htmlspecialchars($_POST['style'],ENT_QUOTES); ?></textarea></td>
    </tr>
    <tr>
    <td>
    <p><b>What message do you wish to convey to your prospective customers?</b>(i.e. what makes your service or product unique)</p>
    <textarea name="message" cols="40" rows="5" id="message"  > <?php echo htmlspecialchars($_POST['message'],ENT_QUOTES); ?></textarea></td>
    </tr>
    <tr>
    <td>
    <p><b>Does your business have branding guidelines(i.e. logo; type size; colours used in publishing and marketing)</p>
    <textarea name="branding" cols="40" rows="5" id="branding"  > <?php echo htmlspecialchars($_POST['branding'],ENT_QUOTES); ?></textarea></td>
    </tr>
    <tr>
    <td>
    <p><b>What is your budget?</b></p>
    <input type="radio" name="budget" value "0" id="budget" />350<br/>
    <input type="radio" name="budget" value "1" id="budget" />350-650<br/>
    <input type="radio" name="budget" value "2" id="budget" />650-1000<br/></td>
    </tr>
    <tr>
    <td>
    <p><b>Can you provide your own images of your business re structure, products and services?</b></p>
    <input name="images" type="radio" value  "0" id="images" />
    Yes<br/>
    <input type="radio" name="images" value "1" id="images" />No<br/></td>
    </tr>
    <tr>
    <td>
    Your name:
    <input type="text" size="60" name="name" value=" <?php echo htmlspecialchars($_POST['name'],ENT_QUOTES); ?>" id="name" />
    </td>
    </tr>
    <tr>
    <td>
    Your email:
    <input type="text" size="40" name="email" value=" <?php echo htmlspecialchars($_POST['email'],ENT_QUOTES); ?>" id="email" />
    </td>
    </tr>
    </table>
    <tr>
    <td>
    <input type="submit"  name="submit" value="Get your Quote!" />
    </td>
    </tr>
    </form> 
    </div> <!--end form-->


      <div id="Layer5"><img src="myimages/offer2.gif" alt="Domains from 60p per month!!" width="124" height="124" />
        <div id="Layer6">
          <a href="http://www.the-bee-scene.com/hostpay"><h3 align="center" style="color: #ffffff">&nbsp;</h3>
          </a></div><!--end layer6-->
      </div><!--end layer5-->
       <div id="Layer10"><img src="myimages/offer2.gif" alt="Marketing applications to get your Business seen!" width="124" height="124" />
          <div id="Layer11">
          <a href="promotion.php"> <h3 align="center" style="color: #ffffff">&nbsp;</h3>
          </a></div><!--end layer10-->
        </div><!--end layer11-->
     <div id="Layer8"><img src="myimages/offer2.gif" alt="Small business Hosting packages from &pound;4.99" width="131" height="124" />
          <div id="Layer9">
             <a href="http://www.the-bee-scene.com/hostpay"><h3 align="center" style="color: #ffffff">&nbsp;</h3>
             </a>
          </div><!--end layer8-->
      </div><!--end layer9
     -------------end Position absolute layers------------------------------------------------------>
    </div><!--end mainbody-->

    </div><!--end wrapper-->
    </body>
    </html>
    In the database, (I use phpmyadmin) id is the primary key. it also auto-increments. the radio buttons are set at tinyint(1). the text area questions are set at text. Name and email are set at varchar. All are not null. Default is set at none for all.

    Any ideas?

    Thanks

  2. #2
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does the script throw any errors? What is the value of $result from running mysqli_query()? I would assume for non-select statements that the function would return the number of rows affected.

    Cheers,
    D.

  3. #3
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure whether it's related, but all your radio buttons look a bit strange:

    Code:
    <input type="radio" name="budget" value "0" id="budget" />&#163;350
    first, there's a = missing between value and the value, and secondly, IDs need to be unique, which means no two radio inputs should've have the same id (simply put a unique number behind it, e.g. budget1, budget2, ...) - the name is and needs to be the same though across all instances, so you can keep it budget there.
    Dan G
    Marketing Strategist & Consultant

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,032
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    echo the query then run the echoed query against the database in PHPMyAdmin. What is the error messages returned if any?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  5. #5
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    disgracian said:

    Does the script throw any errors? What is the value of $result from running mysqli_query()? I would assume for non-select statements that the function would return the number of rows affected.

    The script doesnt throw any errors. the result is it takes the user to brief_result.php which is what it should do.

    c2uk wrote:

    Not sure whether it's related, but all your radio buttons look a bit strange:

    <input type="radio" name="budget" value "0" id="budget" />350first, there's a = missing between value and the value, and secondly, IDs need to be unique, which means no two radio inputs should've have the same id (simply put a unique number behind it, e.g. budget1, budget2, ...) - the name is and needs to be the same though across all instances, so you can keep it budget there.
    Thanks for this imfo i was struggling with the radio buttons, but having made the changes and tested it it there is no difference overall.

    SpacePheonix wrote:

    echo the query then run the echoed query against the database in PHPMyAdmin. What is the error messages returned if any?
    Can you explain how i would echo it against the phpmyadmin database? never done this before.

    thanks again

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,032
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cass27 View Post
    hi

    disgracian said:



    The script doesnt throw any errors. the result is it takes the user to brief_result.php which is what it should do.

    c2uk wrote:



    Thanks for this imfo i was struggling with the radio buttons, but having made the changes and tested it it there is no difference overall.

    SpacePheonix wrote:



    Can you explain how i would echo it against the phpmyadmin database? never done this before.

    thanks again
    Add after the line
    PHP Code:
    $query "INSERT into briefing_form ( 
    primary_function,go_live,biz_type,market,style,message,branding,budget,images,name,email)Values('
    {$primary_function}','{$go_live}','{$biz_type}','{$market}','{$style},'{$message}','{$branding}','{$budget}','{$images}','{$name}','{$email}')"
    Add a line:

    PHP Code:
    echo $query
    Then in PHPMYAdmin when your viewing the database concerned you'll see a number of buttons:

    * Structure
    * SQL
    * Search
    * Query
    * Export
    * Import
    * Operations
    * Privileges
    * Drop

    Click on the SQL button, paste the query into the textarea and click on Go
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  7. #7
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    I inserted the $query (this is going to be useful for future debugging and I recieved this error message:

    #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 '$query = "INSERT into briefing_form (
    primary_function,go_live,biz_type,market,' at line 1

    Could it be I am not allowed to use the words primary? Cant find the problem witht the syntax

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    you submitted this to mysql (via phpmyadmin) --
    Code:
    $query = "INSERT into briefing_form ...
    that's not valid SQL, because it starts with php code

    what you should have submitted is this --
    Code:
    INSERT into briefing_form ...
    see the difference?

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    you submitted this to mysql (via phpmyadmin) --
    Code:
    $query = "INSERT into briefing_form ...
    that's not valid SQL, because it starts with php code

    what you should have submitted is this --
    Code:
    INSERT into briefing_form ...
    see the difference?

    Hi I can see what you mean but actually when i inserted it directly into phpmyadmin I took off $query. I use it on the actual form page.

    However

    I have tracked down the problem! There was a speech mark missing around '{$style} . OWWWW! Anyway thanks for everyones help. It may have only been a speech mark but I've learnt a hell of a lot.


Bookmarks

Posting Permissions

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