1

I'm new to php so I've been trying to make an php page which adds info to Mysql database. But when ever I press submit on the form it directs to the php page and nothing happens. Blank page.

Here's what I have so far.

The form

<table class="table" >
  <h2>Resturants</h2>
  <tbody>
    <form data-toggle="validator" enctype="multipart/form-data" action="editRestaurants.php" method="post">
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Title :</label></td>
        <td><input type="text" class="form-control" id="title" name="title" required></td>
      </tr>
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Text :</label></td> 
        <td><textarea style="width:300px;height:100px" class="form-control" id="text" name="text" required></textarea><br/></td>
      </tr>
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Link :</label></td> 
        <td><input type="text" class="form-control" id="link" name="link" required></td>
      </tr>
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Photo:</label></td>
        <td><input type="file" id="image" class="form-control" name="image" accept="image/jpeg" required></td>
      </tr>
      <tr>
        <td><input type="submit" value="Submit" style="font-size:17px;" id="submit"></td>
      </tr>
    </form>
  </tbody>
</table>

And here's the php file

include('connection.php'); 
session_start(); 
$title=$_POST['title']; 
$text=$_POST['text']; 
$link=$_POST['link']; 
$image=$_FILES['image']; 
$imagename=$image['name']; 
if(empty($imagename)) 
{ 
    $imagename="defualt.jpg"; 
} 
if(!empty($title) && !empty($text)) 
{ 
    $query=mysqli_query($GLOBALS["___mysqli_ston"], 'INSERT INTO `resturant`(`ID`, `Title`, `ImagePath`, `Text`, `Link`, `Date`) VALUES (NULL,'$title','$imagename','$text','$link',CURRENT_DATE())');


    if(!empty($image)) 
    { 
    $target_dir= "NewsImages/"; 
    $target_file = $target_dir . basename($imagename); 
    move_uploaded_file($image["tmp_name"], $target_file); 

    } 

    $_SESSION['status']="Successful"; 
} 
else 
{ 
    $_SESSION['status']="Please check all fields"; 

} 

header('Location:cms.php'); 
9
  • 1
    Please do not use mysql_* functions. Commented Mar 15, 2016 at 15:20
  • Then what should I use? Commented Mar 15, 2016 at 15:21
  • Use either MySQLi or PDO please. Commented Mar 15, 2016 at 15:22
  • I have a whole website based up on Mysql! how can I change it all? Commented Mar 15, 2016 at 15:33
  • See this website for more information: bobby-tables.com Commented Mar 15, 2016 at 15:34

3 Answers 3

1

There's a syntax error with your $query. The following code would work:

$query=mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO `resturant`(`ID`, `Title`, `ImagePath`, `Text`, `Link`, `Date`) VALUES (NULL,'$title','$imagename','$text','$link',CURRENT_DATE())");

I've simplified your code. Now, it will check if the file is successfully uploaded before redirecting to cms.php.

if(empty($imagename)) { 
    $imagename="defualt.jpg";
}

if(!empty($title) && !empty($text)) {
    $query=mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO `resturant`(`ID`, `Title`, `ImagePath`, `Text`, `Link`, `Date`) VALUES (NULL,'$title','$imagename','$text','$link',CURRENT_DATE())");

    $target_dir= "NewsImages/"; 
    $target_file = $target_dir . basename($imagename);

    if(move_uploaded_file($image["tmp_name"], $target_file)) {
        $_SESSION['status']="Successful";
        header('Location:cms.php');
        exit;
    } else {
        $_SESSION['status']="Something went wrong while uploading the image.";
    }

} else { 
    $_SESSION['status']="Please check all fields";
}
Sign up to request clarification or add additional context in comments.

Comments

0
==========================[START OF HTML]============================
|<table class="table" >
  <h2>Resturants</h2>
  <tbody>
    <form data-toggle="validator" enctype="multipart/form-data" action="editRestaurants.php" method="post">
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Title :</label></td>
        <td><input type="text" class="form-control" id="title" name="title" required></td>
      </tr>
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Text :</label></td> 
        <td><textarea style="width:300px;height:100px" class="form-control" id="text" name="text" required></textarea><br/></td>
      </tr>
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Link :</label></td> 
        <td><input type="text" class="form-control" id="link" name="link" required></td>
      </tr>
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Photo:</label></td>
        <td><input type="file" id="image" class="form-control" name="image" accept="image/jpeg" required></td>
      </tr>
      <tr>
        <td><input type="submit" value="Submit" style="font-size:17px;" id="submit" name="submit"></td>
      </tr>
    </form>
  </tbody>
</table>
=====================[END OF HTML]========================
===========================================================
======================[START OF PHP]=======================
<?php
include('connection.php');
session_start();
$title=$_POST['title'];
$text=$_POST['text'];
$link=$_POST['link'];
$image=$_FILES['image'];
$imagename=$image['name'];
$submit=$_FILES['submit'];
if(isset($_POST['submit'])){
if(empty($imagename))
{
    $imagename="defualt.jpg";
}
if(!empty($title) && !empty($text))
{
    $query=mysql_query('INSERT INTO `resturant`(`ID`, `Title`, `ImagePath`, `Text`, `Link`, `Date`) VALUES (NULL,"'.$title.'","'.$imagename.'","'.$text.'","'.$link'",CURRENT_DATE())');
    if(!empty($image))
    {
    $target_dir= "NewsImages/";
    $target_file = $target_dir . basename($imagename);
    move_uploaded_file($image["tmp_name"], $target_file);
    }
    $_SESSION['status']="Successful";
}
else
{
    $_SESSION['status']="Please check all fields";
}
header('Location:cms.php');
}
?>
=========================[END OF PHP ]=============================

Comments

0
I read your code and try to run it. try this code. It will help you           

1) connection.php

            <?php
            $con = mysqli_connect("localhost","root","","resturant");
            ?>

       2) form.php

        <?php  
        session_start();
        include('connection.php');
        if($_POST['submit']){ 

        echo $title=$_POST['title']; echo "<br>";
        echo $text=$_POST['text']; echo "<br>";
        echo $link=$_POST['link']; echo "<br>";
        echo $image=$_FILES['image']['name']; echo "<br>";


        if(!empty($title) && !empty($text) && !empty($image))
        { 

        $sql =  "INSERT INTO resturant(Title, ImagePath, Text, Link, Date) VALUES ('$title','$image','$text','$link',CURRENT_DATE())";

            $query = mysqli_query($con,$sql) or die(mysql_error());




             $path="NewsImages/$image";
             move_uploaded_file($_FILES['image']['tmp_name'],$path);




            $_SESSION['status']="Successful"; 
        } 
        else 
        { 
            $_SESSION['status']="Please check all fields"; 

        } 
        header('Location:cms.php');
        }



        ?>

        <table class="table">
          <h2>Resturants</h2>
          <tbody>
            <form data-toggle="validator" enctype="multipart/form-data" action="" method="post">
              <tr>
                <td><label for="inputName" class="control-label" style="font-size:17px;">Title :</label></td>
                <td><input type="text" class="form-control" id="title" name="title" required></td>
              </tr>
              <tr>
                <td><label for="inputName" class="control-label" style="font-size:17px;">Text :</label></td> 
                <td><textarea style="width:300px;height:100px" class="form-control" id="text" name="text" required></textarea><br/></td>
              </tr>
              <tr>
                <td><label for="inputName" class="control-label" style="font-size:17px;">Link :</label></td> 
                <td><input type="text" class="form-control" id="link" name="link" required></td>
              </tr>
              <tr>
                <td><label for="inputName" class="control-label" style="font-size:17px;">Photo:</label></td>
                <td><input type="file" id="image" class="form-control" name="image" accept="image/jpeg" required></td>
              </tr>
              <tr>
                <td><input type="submit" value="Submit" style="font-size:17px;" name="submit"></td>
              </tr>
            </form>
          </tbody>
        </table>





    3) cms.php

    <?php

    session_start();

    echo $_SESSION['status'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.