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
https://forums.phpfreaks.com/topic/296600-insert-not-working/
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
https://forums.phpfreaks.com/topic/296600-insert-not-working/#findComment-1513030
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.

Link to comment
https://forums.phpfreaks.com/topic/296600-insert-not-working/#findComment-1513033
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
https://forums.phpfreaks.com/topic/296600-insert-not-working/#findComment-1513035
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.