0

I stripped down query for only one insert

<?php
session_start();
include 'cstring.php';
$title="";
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
    $title=$_POST['title']; 
    $query=mysqli_query($con,"insert into blogpages(blogpagetitle) values('".$title."')");
    if($query){
        $bloga="sucessfully added a new blog";
        echo $bloga; 
     }
     else {
        echo mysqli_error($con); // if using mysqli do not use mysql in between
     }
}
mysqli_close($con);
?>

is there something wong in this code that it doesnt insert into mysql

table structure

1.bpid int(50)--------------null-no default-none autoincrement

2.blogpagetitle------------varchar(255) utf16_general_ci

3.datemade-------------timestamp current time stamp

4.blogpagedescription---------text utf16_general_ci

5.blogbody----------------longtext utf16_general_ci

6.blogpageextended------------ text utf16_general_ci

8
  • 1
    What is the structure of your blogpages table? Commented Nov 10, 2014 at 11:14
  • To display errors correctly you need to have: print mysqli_error($con); Commented Nov 10, 2014 at 11:14
  • 1
    Also I really hope this code is much simplified for SO. NEVER take a post value and insert it straight into a DB, always use something like: $title = mysqli_real_escape_string($_POST['title']); Commented Nov 10, 2014 at 11:16
  • no errors displayed or query inserted. Commented Nov 10, 2014 at 11:28
  • 1
    To print the error you should use mysqli_error($con) and not mysql_error(). Commented Nov 10, 2014 at 11:32

2 Answers 2

1

TIP

  1. Sanitize variables, Use mysqli_real_escape_string()
  2. When you are not able to debug your code, echo every possible stuff and die the rest code. For example here, echo if there is error in DB connection, echo the query to see if it is correct, echo the result of query execution, echo if there is some error!
Sign up to request clarification or add additional context in comments.

2 Comments

No no. echo mysql_error();} is closing else { $title=$_POST['title']; Check it yourself.
indentation! God! Let me keep the tips and delete the answer!
0

You should be using echo mysqli_error($con) to get the error message rather than mysql_error().

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.