Jump to content

MySqli_query not submiting the Query


reisve
Go to solution Solved by reisve,

Recommended Posts

Hi

 

I have the following code, but can't get the data inserted in the database. Can someone help me?

<?php
  if ($_POST) {
    $comp_cat = $_POST['category']; 
    $comp_name = $_POST['company']; 
    $comp_link = $_POST['link']; 
    $comp_desc = $_POST['description']; 
    $comp_addr = $_POST['address']; 
    $comp_county = $_POST['county']; 
    $comp_city = $_POST['city']; 
    $comp_state = $_POST['state']; 
    $comp_country = $_POST['country']; 
    $comp_duration = $_POST['duration']; 
    $comp_date = date("Y-m-d");
  }

  if ($_FILES) {
    $allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
    $max_filesize = 1000240;
    $upload_path = 'userpics/';
    
    $comp_logo = $_FILES['file']['name'];
    $ext = substr($comp_logo, strpos($comp_logo,'.'), strlen($comp_logo)-1);

    if(!in_array($ext, $allowed_filetypes))
      die('The file you attempted to upload is not allowed.');
    
    if(filesize($_FILES['file']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');
    
    if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');
    
    if(move_uploaded_file($_FILES['file']['tmp_name'],$upload_path . $comp_logo)) {

      $link = mysqli_connect($host, $user, $pass, $db);
      
      /* check connection */
      if (mysqli_connect_errno()) {
        $msg = $error['dbconnection'];
        $page = '../../index.php';
        header("Location: templates/frontend/error.php?msg=$msg&page=$page");
        exit();
      }
   
      $query = "INSERT INTO links (comp_cat, comp_name, comp_link, comp_desc, comp_addr, comp_county, comp_city, comp_state, comp_country, comp_logo, comp_date)";
      $query .= " VALUES ($comp_cat, '$comp_name', '$comp_link', '$comp_desc', '$comp_addr', '$comp_county', '$comp_city', '$comp_state', '$comp_country', '$comp_duration', '$comp_date', '$comp_logo')"; 
      if (mysqli_query($link, $query)){
        echo "inserted";  
      } else {
        mysqli_error($link);
      }

      echo 'Your file upload was successful!';

    } else {
      echo 'There was an error during the file upload.  Please try again.';
    }
}
?>
  <div class = "centercontent">
    <div class = "centerheader">
      <?php echo $addlink['title']; ?>
    </div>                            
    <div class = "centertext">
      <form action = "addlink.php" enctype = "multipart/form-data" method="post" id = "addlinkform">
        <table border = "0" width = "100%">
          <tr>
            <td width = "37%" align = "right" valign = "top"><label><?php echo $addlink['category']; ?></label></td>      
            <td width = "63%">
              <select id = "categories" name = "category">
                <?php
                
                  $link = mysqli_connect("localhost", "root", "palhaco", "negocios_angola");
                  if (!$link) {
                      die('Could not connect: ' . mysqli_connect_error());
                  }
            
                  $sql = "SELECT cat_id, cat_name, cat_parent FROM categories";
                  $res = mysqli_query($link, $sql);
                  while (list($id, $name, $parent) = mysqli_fetch_row($res)) {
                      $data[$parent][] = array('id'=>$id, 'name'=>$name);
                  }
                  
                  // function to print a category then its child categories 
                  function displayHierarchy(&$arr, $parent, $indent=0)
                  {
                    if (isset($arr[$parent])){
                      foreach($arr[$parent] as $rec){
                        if ($indent == 0){
                        } elseif ($indent == 1){
                          echo "<option value = " . $rec['id'] . ">" . $rec['name'] . "</option>" ;
                         }else {
                          echo "<option value = ". $rec['id'] . ">" . str_repeat("  |", $indent) .  "___" .$rec['name'] . "</option>" ;
                        }
                        displayHierarchy($arr, $rec['id'], $indent+1);
                      }
                    }
                  }
                
                  // call the recursive function
                  displayHierarchy($data, '');
                ?>
              </select>
            </td>
          </tr>
          <tr>
            <td colspan = "2" height = "10px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['name']; ?></label></td>      
            <td><input name = "company" type = "text"></td>
          </tr>
          <tr>
            <td colspan = "2" height = "5px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['link']; ?></label></td>      
            <td><input name = "link" type = "text"></td>
          </tr>
          <tr>
            <td colspan = "2" height = "10px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['desc']; ?></label></td>      
            <td><textarea name = "description"></textarea></td>
           </tr>
          <tr>
            <td colspan = "2" height = "5px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['addr']; ?></label></td>      
            <td><input name = "address" type = "text"></td>
          </tr>
          <tr>
            <td colspan = "2" height = "5px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['county']; ?></label></td>      
            <td><input name = "county" type = "text"></td>
          </tr>
          <tr>
            <td colspan = "2" height = "5px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['city']; ?></label></td>      
            <td><input name = "city" type = "text"></td>
          </tr>
          <tr>
            <td colspan = "2" height = "5px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['state']; ?></label></td>      
            <td><input name = "state" type = "text"></td>
          </tr>
          <tr>
            <td colspan = "2" height = "5px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['country']; ?></label></td>      
            <td><input name = "country" type = "text"></td>
          </tr>
          <tr>
            <td colspan = "2" height = "5px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['duration']; ?></label></td>      
            <td>
            <select name = "duration">

            <?php
              $sql = "SELECT * FROM duration";
              $result = mysqli_query($link, $sql);
              while ($row = mysqli_fetch_assoc($result)) {
                echo "<option value = "  . $row['dur_id'] . ">" . $row['duration'] . $addlink['days'] . " - " . $row['price'] . " USD</option>";                 
              }
              mysqli_free_result($res);
              mysqli_close($link);
            ?>

            </select>
            </td>                          
          </tr>
          <tr>
            <td colspan = "2" height = "5px"></td>
          </tr
          <tr>
            <td align = "right" valign = "top"><label><?php echo $addlink['logo']; ?></label></td>      
            <td><input type="file" name="file"></td>
          </tr>
          <tr>
            <td colspan = "2" height = "5px"></td>
          </tr
          <tr>      
            <td colspan = "2" align = "center"><button name = "submit" type = "submit"><?php echo $addlink['submit']; ?></button></td>
          </tr>
        </table>
      </form>
    </div>
  </div>

The only message I get is: "Your file upload was successful!", but in the data base there is nothing.

 

Thank You.

 

P.S. This is just a module for a bigger project

 

Link to comment
Share on other sites

Don't break up your $query variable....

$query = "INSERT INTO links (comp_cat, comp_name, comp_link, comp_desc, comp_addr, comp_county, comp_city, comp_state, comp_country, comp_logo, comp_date)";

$query .= " VALUES ($comp_cat, '$comp_name', '$comp_link', '$comp_desc', '$comp_addr', '$comp_county', '$comp_city', '$comp_state', '$comp_country', '$comp_duration', '$comp_date', '$comp_logo')"; 

if (mysqli_query($link, $query)){

}

Your INSERT statement and VALUES are all one long command. Right now your $query variable is set to the second $query variable. Also, do what requinix says and use prepare()  :rtfm:  :anim_reading:  :anim_rules:  :hail_freaks:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.