-2

I am able to connect to mysql database when i checked the connection but i was unable to retrieve the data. I am new to php and mysql. I would really appreciate if someone could help me in this.

Here's my code:

<?php
$conn = mysqli_connect($servername, $username, $password);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

if (isset($_POST['submit'])) {
    $name=$_POST['name'];
    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $message=$_POST['message'];
    $result=mysql_query("INSERT INTO contact('user','email','subject','message') VALUES ('$name','$email','$subject','$message')");

if ($result) {
    $message="successfully sent the query!!";
}
else
    {$message="try again!!";}
}

?>

<form action="index.php" method="post" class="form-horizontal">
    <div class="form-group">
        <label for="name" class ="col-lg-2 control-label" > Name</label>
        <div class="col-lg-7">
            <input type="text" name="name" class="form-control" id ="name" placeholder="Enter your Name">
         </div>
    </div>
</form>
</div>
<div class="col-lg-1">

</div>
<div >
    <form action="index.php" method="post" class="form-horizontal">
         <div class="form-group">
             <label for="email" class ="col-lg-2 control-label" > Email</label>
             <div class="col-lg-7">
                 <input type="text" name="email" class="form-control" id ="email" placeholder="Enter your email address">
             </div>
         </div>
    </form>
</div> <div class="col-lg-1">

</div>
<div >
     <form action="index.php" method="post" class="form-horizontal">
          <div class="form-group">
              <label for="subject" class ="col-lg-2 control-label" > Subject</label>
              <div class="col-lg-7">
                   <input type="text" name="subject" class="form-control" id ="subject" placeholder="Your Subject">
              </div>
          </div>
     </form>
</div>
<div class="col-lg-1">

</div>
<div>
     <form action="index.php" method="post" class="form-horizontal">
         <div class="form-group">
             <label for="message" class ="col-lg-2 control-label" > Message</label>
             <div class="col-lg-7">
                 <textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
              </div>
          </div> <!-- end form -->
          <div class="col-lg-1">

          </div>
          <div class="form-group">
              <div class="col-lg-7 col-lg-offset-2">
                 <button type="submit" name="submit" class="btn btn-primary">Submit</button>
              </div>
          </div>
    </form>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
epskip
  • 37
  • 1
  • 8
  • 1
    You're mixing `mysql_*` and `mysqli_*` functions. That won't work. – Jay Blanchard Oct 21 '15 at 19:43
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – Jay Blanchard Oct 21 '15 at 19:44
  • 4 major errors here Sam @JayBlanchard possibly more. – Funk Forty Niner Oct 21 '15 at 19:44
  • *Yessir Ralph* Time to call in the Sheriff @Fred-ii-. The code Sheriff, that is. – Jay Blanchard Oct 21 '15 at 19:47
  • 1
    @JayBlanchard Remember, he shot the sheriff, but not the deputy Sam ;-) I risk in getting shot at the OK Corral. – Funk Forty Niner Oct 21 '15 at 19:49
  • `quotes != backticks` for column and table names. Let's see how long it takes the vampire to add this @Fred-ii- – Jay Blanchard Oct 21 '15 at 19:49
  • Ohhhhhhhh good catch there Sam! @JayBlanchard see? I was right.... "possibly more" ;-) – Funk Forty Niner Oct 21 '15 at 19:50
  • Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Oct 21 '15 at 19:52
  • you keep overwriting your original question instead of marking them as additional edits. That's not appreciated by many here. – Funk Forty Niner Oct 21 '15 at 20:10
  • there are issues in the page and ur explanation... #1. you never mentioned which form u targetting since first 2 forms does not have submit name. #2. form 3 has name="submit" but its type button make it type and see. #3. write like this mysql_query($query) or die(mysql_error()); this will throw any mysql error if the query has. – Ahmad Oct 21 '15 at 20:16
  • #4. you need to seprate the html and php thing to learn at stage 1 :) put php in another page and when you send with form submission use ";print_r($_POST);?> this will print all the incoming post variables and you will be sure that html part is working fine. Then you can instead of post use $_REQUEST in the php and directly call that page like http://localhost/insert.php?submit=1&name=username&email=email and see what it does . #5. you can use echo for query to see if its reaching there. i hope you will be able to fix the issue :) – Ahmad Oct 21 '15 at 20:17
  • 2
    **Dont change the scope or content of the question after it has been answered. It makes the answers look like nonsence, and they are therefore no use to others that have similiar problems to your ORIGINAL question. I will revert the edit if you do not do it yourself!** If necessary ask another question – RiggsFolly Oct 21 '15 at 20:23

1 Answers1

2

Final (?) UPDATE

You can follow this approach until you learn PDO.

<?php
$conn = mysqli_connect($servername, $username, $password, $db_name);// Establishing Connection with Server
mysqli_set_charset($conn, 'utf8');
if (!$conn) {
    die("Database connection failed: " . mysqli_error($conn));
}

echo "Connected successfully";

if (isset($_POST['submit'])) {

    //Escaping string, not 100% safe, also consider validating rules and sanitization
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $subject = mysqli_real_escape_string($conn, $_POST['subject']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);
    $result = mysqli_query($conn, "INSERT INTO contact (user, email, subject, message) VALUES ('$name', '$email', '$subject', '$message');");

    if ($result) {
        $message = "successfully sent the query!!";
    } else {
        $message="try again!!";
    }
}
?>

<form action="index.php" method="post" class="form-horizontal">
    <fieldset>
        <div class="form-group">
            <div class="col-lg-7">
                <input type="text" name="name" class="form-control" id ="name" placeholder="Enter your name">
            </div>
            <label for="email" class="control-label">Email</label>
            <div class="col-lg-7">
                <input type="text" name="email" class="form-control" id ="email" placeholder="Enter your email address">
            </div>
            <label for="subject" class ="control-label">Subject</label>
            <div class="col-lg-7">
                <input type="text" name="subject" class="form-control" id ="subject" placeholder="Your Subject">
            </div>
            <label for="message" class ="control-label">Message</label>
            <div class="col-lg-7">
                <textarea name="message" class="form-control" id ="message" cols="20" rows="3" placeholder="Your Message"></textarea>
            </div>
            <button type="submit" name="submit" class="btn btn-primary">Submit</button>
        </div>
    </fieldset>
</form>


OLD

You use both mysql_* and mysqli_* functions here. Also you have to sanitize and validate your inputs. Try to use PDO instead. For this snippet of code try the following:

Change:

$conn = mysqli_connect($servername, $username, $password);

$result=mysql_query("INSERT INTO contact ('user', 'email', 'subject', 'message') VALUES ('$name', '$email', '$subject', '$message')");

with this:

$conn = mysqli_connect($servername, $username, $password, $db_name);

$result = mysqli_query($conn, "INSERT INTO contact(user, email, subject, message) VALUES ('$name','$email','$subject','$message')");

Also, at least use:

$name = mysqli_real_escape_string($connection, $_POST['name']);

//also for rest of your $_POST variables

Furthermore you need to use only one html form that includes all your inputs.

Kostas Mitsarakis
  • 4,772
  • 3
  • 23
  • 37