user6239042
user6239042

Reputation:

How to insert data in mysql table randomly

I have created a script that get data from other website and insert or add it in mysql table. When I use to get data from website, It runs correctly but when I insert it in mysql I cannot do that thing. it shows me error, i.e.

 Error: INSERT INTO body (title) VALUES (Offworld Trading Company Free Download)

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Trading Company Free Download)' at line 2Error: INSERT INTO body (body) VALUES (

Here is my code see what am I doing wrong.

require_once('simple_html_dom.php');
$html = new simple_html_dom();
$xml = simplexml_load_file("sitemap.xml");
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "kuta";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
foreach ($xml->url as $s) {
    $s = $s->loc;
    $html = file_get_html($s);
    $element = $html->find('div[class=post-content clear-block]');
    // Find all links 
    $vez = $html->find('div[class=post-date]');
    $p = '|<a [^>]*href="http://<Some Url>[^"]*"[^>]*>.*</a>|iU';
    $h = $html->find('h1[class=title]');
    if (empty($vez)) {
        foreach ($h as $ha) {

            $q = strip_tags($ha->plaintext);
            $sql = "INSERT INTO body (title) VALUES ($q)";
            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }
        foreach ($element as $a) {
            if ($a === end($element)) {
                $m = preg_replace($p, '', $a);
            }
            $m = strip_tags($m, '<p><a><img><br /><br><div>');
            $sql = "INSERT INTO body (body) VALUES ($m)";
            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }
    }
}
$conn->close();

I have created table named body, all the things are going well, When I insert data like

    $sql = "INSERT INTO MyGuests (title, body)
VALUES ('Aditya', 'pandey')";

Show me where am I wrong and correct me. Your answer are highly appreciable, Please help me.

Upvotes: 2

Views: 102

Answers (2)

vibhav yadav
vibhav yadav

Reputation: 53

Try these codes;

  $sql = "INSERT INTO body (`title`) VALUES ('$q')";

and,

$sql = "INSERT INTO body (`body`) VALUES ('$m')";

Hope this helps.

Upvotes: 0

vaso123
vaso123

Reputation: 12391

When you inserting values into string fields, the right MySql syntax is to wrap them around with a quote '

For example:

$sql = "INSERT INTO body (body) VALUES ('$m')";

Do it also for $q and all the strings. I suggest you to escape them with mysqli_real_escape_string function to avoid sql injection and syntax errors.

Upvotes: 2

Related Questions