1

I'm developing a web platform. I'm using PHP and MySQL. I want to insert data to db. My code below.

<?php
session_start();
require_once('../../system/database.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$ownerid     = (int)$_SESSION['id'];

$record_time = date('H:i:s');
$record_date = date('Y-m-d');

// form_data
$name        = strip_tags($_POST['name']);
$surname     = strip_tags($_POST['surname']);
$phone1      = strip_tags($_POST['phone1']);
$birthday    = strip_tags(trim($_POST['birthday']));
$gender      = strip_tags(trim($_POST['gender']));
$company     = strip_tags(trim($_POST['company']));
$address1    = strip_tags(trim($_POST['address1']));
$address2    = strip_tags(trim($_POST['address2']));
$phone2      = strip_tags(trim($_POST['phone2']));
$mail1       = strip_tags(trim($_POST['mail1']));
$mail2       = strip_tags(trim($_POST['mail2']));
$about       = strip_tags(trim($_POST['about']));
$type_of     = strip_tags(trim($_POST['type_of']));
$visible     = strip_tags(trim($_POST['visible']));

$query = "INSERT INTO contact (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES('$ownerid', '$name', '$surname', '$birthday', '$gender', '$address1', '$address2', '$phone1', '$phone2', '$mail1', '$mail2', '$mail1', '$mail2', '$about', '$type_of', '$visible', '$record_time', '$record_date')";
$result = mysqli_query($connection, $query);
mysqli_error($connection);

} else {
header('Location: ../new_contact.php');
}

But my MySQL code does not work and write any error message!

3
  • there is some integer value even you have tired '' make sure it without single quotas Commented Mar 21, 2017 at 8:45
  • 1
    echo the $query and run it directly in phpmyadmin for better feedback Commented Mar 21, 2017 at 8:46
  • Because you are using the variable as a string in your query. You should use concatinate Commented Mar 21, 2017 at 8:46

4 Answers 4

1

wrong number of column in values clause (you repeat two time mail1 and mail2 ) try

$query = "INSERT INTO contact 
         (ownerid, name, surname, birthday, gender, address1, address2, phone1,
   phone2, mail1, mail2, about, type_of, visible, time, date) 
VALUES('$ownerid', '$name', '$surname', '$birthday', '$gender', '$address1', '$address2', '$phone1', 
  '$phone2', '$mail1', '$mail2', '$about', '$type_of', '$visible', '$record_time', '$record_date')";
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god! Thanks!
1

Edit insert variables to be like this

$query = "INSERT INTO contact (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES(".$ownerid.", '".$name."', '".$surname."', '".$birthday."', '".$gender."', '".$address1."', '".$address2."', '".$phone1."', '".$phone2."', '".$mail1."', '".$mail2."', '".$about."', '".$type_of."', '".$visible."', '".$record_time."', '".$record_date."')";

Comments

0

You need to concate PHP variable properly and also need correct query format. Try this

$query = "INSERT INTO `contact` (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES('".$ownerid."','".$name."', '".$surname."', '".$birthday."','".$gender."', '".$address1."', '".$address2."', '".$phone1."','".$phone2."', '".$mail1."','".$mail2."', '".$about."', '".$type_of."', '".$visible."', '".$record_time."', '".$record_date."')";

Comments

0

If you have access to PHPMyAdmin, place the query in the SQL section and test it. Sometimes it can give you clues as where to look for issues.

As ScaisEdge says, you have 16 entries as keys, but 18 entries as values into your insert. It can be helpful to use coding software when using PHP that highlights like words (notepad++ is free and does this) Eclipse is another. Also, place your query into PHPMyAdmin "SQL" and test your query to see if it gives you any results or throws an error. It will point where in the string to start looking for your error.

1 Comment

IMPORTANT NOTE: When a string is specified in double quotes or with heredoc, variables are parsed within it. php.net/manual/en/…

Your Answer

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