Jump to content

Insert not working


darish

Recommended Posts

 

<tr>
                          <th width="40%">Nome da categoria</th>
                          <th><input type="text"  name="pname"class="form-control" placeholder="Introduz o Titulo da Descrição" onKeyPress="return alpha(event)" style="text-transform:lowercase;" required>
        <span id="error" style="color: Red; display: none">* Special Characters not allowed</span>
        
   <script>
function alpha(e) {
    var k;
    document.all ? k = e.keyCode : k = e.which;
    return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
}
</script>
        </th>
                        </tr>
                        
                      
                        <tr>
                          <th>Descriçao Categoria</th>
                          <td><input type="text" name="pdes" class="form-control" placeholder="Introduz a Descrição da categoria" required></td>
                        </tr>
                        
                       
                        
                        <tr>
                          <th width="40%"><i class="icon_profile"></i>Image</th>
                          <td><input type="file" name="image" required></td>
                        </tr>
                      </tbody>
                    </table>
<header class="panel-heading"><div align="center"><button type="submit" class="btn btn-primary">Adicionar</button></div></header>
 
 
</form>

 

Its not working, can someone tell me , whats happening? 

Link to comment
Share on other sites

Yeah, I guess I dont have the insert code ...

<?php
if(isset($_POST['enviar']) && $_POST['enviar']=="guardar"){
$pdes=$_POST['pdes'];
                $pname=$_POST['pname']; 
                $image=$_POST['image']; 
 
$result = mysql_query("insert into categorias(pdes, pname, image) values ('$pdes', '$pname', '$image')") or die (mysql_error());
              
                 if($result){
                 echo "<meta http-equiv=\"refresh\" content='0; url=?pg=4&msg=1'>", exit;
                 }
                 else{
                 echo "<meta http-equiv=\"refresh\" content='0; url=?pg=4&msg=2'>";exit;
                 }
 
} ?>

 

I decided to put this code, but still not working, can someone give me a insert code ? So I can do it? 

 

 

it only got 3 variables .. pname / pdes / image 

Link to comment
Share on other sites

Here is a headstart for you.

<form action="" method="post" enctype="multipart/form-data">
<tr>
                          <th width="40%">Nome da categoria</th>
                          <th><input type="text"  name="pname"class="form-control" placeholder="Introduz o Titulo da Descrição" onKeyPress="return alpha(event)" style="text-transform:lowercase;" required>
        <span id="error" style="color: Red; display: none">* Special Characters not allowed</span>
        
   <script>
function alpha(e) {
    var k;
    document.all ? k = e.keyCode : k = e.which;
    return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
}
</script>
        </th>
                        </tr>
                        
                      
                        <tr>
                          <th>Descriçao Categoria</th>
                          <td><input type="text" name="pdes" class="form-control" placeholder="Introduz a Descrição da categoria" required></td>
                        </tr>
                        
                      
                        
                        <tr>
                          <th width="40%"><i class="icon_profile"></i>Image</th>
                          <td><input type="file" name="image" required></td>
                        </tr>
                      </tbody>
                    </table>
<header class="panel-heading"><div align="center"><button type="submit" name="Submit" class="btn btn-primary">Adicionar</button></div></header>
 
 
</form>
<?php
if (isset($_POST['Submit'])) {
    $pdes  = $_POST['pdes'];
    $pname = $_POST['pname'];
    
    if ($_FILES["file"]["error"] > 0)
        echo "Error: " . $_FILES["image"]["error"] . "<br />";
    else {
        echo "Upload: " . $_FILES['image']['name'] . "<br />";
        echo "Type: " . $_FILES['image']['type'] . "<br />";
        echo "Size: " . ($_FILES['image']['size'] / 1024) . " Kb<br />";
        echo "Stored in: " . $_FILES['image']['tmp_name'];
    }
    
    
    $result = mysql_query("insert into categorias(pdes, pname, image) values ('$pdes', '$pname', '" . $_FILES['image']['name'] . "')") or die(mysql_error());
    
    if ($result) {
        echo "<meta http-equiv=\"refresh\" content='0; url=?pg=4&msg=1'>", exit;
    } else {
        echo "<meta http-equiv=\"refresh\" content='0; url=?pg=4&msg=2'>";
        exit;
    }
    
}
?>

You won't see the image data because you redirect, but it's there.

 

You should be using pdo with prepared statements or mysqli functions using mysqli_real_escape_string

 

This is a minimum should be doing, should also be checking image types,sizes,renaming the images(possibly adding timestamp to prevent duplicates), you can not trust anyone with what they insert in forms.

 

You most likely want to add move_uploaded_file() to place the image in a desired directory.

Edited by QuickOldCar
Link to comment
Share on other sites

As for the form part and image upload it's works, I tested it.

 

Are you doing the mysql connection and correct credentials.

Is the table name correct and it's column names, what type of data types?

 

You can try to enable error reporting, place this the very top of your script.

error_reporting(E_ALL);
ini_set('display_errors', '1');
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.