-1

I am trying to add 50 columns of data to mysql database with column names as l1, l2, l3 and so on..

if((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) {
    $rows = explode("\n", $out);
    $x=1;
    foreach($rows as $row) {
        $row = trim($row);

            $rec[$x] = $row;
            $x = $x + 1;
    }
}
$senqu = "INSERT INTO simple (name,l1, l2, l3,l4,l5,l6,l7,l8,l9,l10,l11, l12, l13,l14,l15,l16,l17,l18,l19,l20,l21, l22, l23,l24,l25,l26,l27,l28,l29,l30,l31, l32, l33,l34,l35,l36,l37,3l8,3l9,l40,l41, l42, l43,l44,l45,l46,l47,l48,l49,l50) VALUES (".$abc;
for ($i =1;$i<=49;$i++) {
    $senqu = $senqu.",'".$rec[$i]."'";
}
$senqu = $senqu.") WHERE name = ".$domain;
$db->query($senqu);

the code is giving me following errors:

PHP Notice: Undefined variable: db in /home/dataegud/public_html/zzuse.com/scrips/whois.php on line 346 [06-Apr-2017 15:57:37 Etc/GMT] PHP Fatal error: Call to a member function query() on a non-object in /home/dataegud/public_html/zzuse.com/scrips/whois.php on line 346

Can you help me. I am a novice in php and want to gain some knowledge.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • `VALUES ('$abc'";` – RiggsFolly Apr 06 '17 at 16:05
  • Your script is at risk of an [SQL Injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) [Attack](https://media.giphy.com/media/A78k1Rh3Ical2/giphy.gif) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Apr 06 '17 at 16:06
  • it does say everything, doesn't it? `$db` is not defined. Forgot to include a config file maybe? Forgot to connect to db manually? – Jeff Apr 06 '17 at 16:07
  • Why you are using `Where` in the insert query? It is not possible. – siddiq Apr 06 '17 at 16:07
  • and `".$domain` that stands to be a string and don't know where/how it's defined. – Funk Forty Niner Apr 06 '17 at 16:08
  • 2
    Insert doesn't support where clause. Also try adding bacticks to name. Seems it's a reserved word – Rotimi Apr 06 '17 at 16:09
  • ^ true that; good spot @Akin yeah that thing's failing on them miserably and in more ways than one. – Funk Forty Niner Apr 06 '17 at 16:10
  • Yeah @Fred-ii though the code looks kinda messy. Prepared statements are needed urgently – Rotimi Apr 06 '17 at 16:12
  • apart from the errors in your sql: you're not connecting to db yet, which gives you the errors you received. Something like this: `$db = new mysqli('localhost', 'my_user', 'my_password', 'my_db');` – Jeff Apr 06 '17 at 16:13
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Qirel Apr 06 '17 at 16:15

1 Answers1

0

the variable db is not defined due to which you are getting the error.The following link is causing the error

$db->query

================================================== Following is the query for insert

    <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 
  • ..now $conn is not defined. – Jeff Apr 06 '17 at 16:40
  • connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "
    " . $conn->error; } $conn->close(); ?>
    – nairsumesh1991 Apr 06 '17 at 16:45