Asked  8 Months ago    Answers:  5   Viewed   44 times

I've been looking at this code for a while now and I can't see where the problem is. I have been reading the whole of StackOverflow and still can't see where my error is.

<?php

mysqli_connect("localhost","root","","web_table");
mysql_select_db("web_table") or die(mysql_error());

if (mysqli_connect_errno()) {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

}
echo "<p> Connection Successful!"

mysqli_query('INSERT INTO web_formitem (ID, formID, caption, key, sortorder, type, enabled, mandatory, data) VALUES (105, 7, Tip izdelka (6), producttype_6, 42, 5, 1, 0, 0)');


echo "<p>Insert successfull";

?>

The error is somewhere in line 13, thats mysqli_query('insert.... I tried to help myself with http://www.w3schools.com/php/php_mysql_insert.asp but it's not helping me much.

 Answers

23

Warning: Never ever refer to w3schools for learning purposes. They have so many mistakes in their tutorials.

According to the mysqli_query documentation, the first parameter must be a connection string:

$link = mysqli_connect("localhost","root","","web_table");

mysqli_query($link,"INSERT INTO web_formitem (`ID`, `formID`, `caption`, `key`, `sortorder`, `type`, `enabled`, `mandatory`, `data`)
VALUES (105, 7, 'Tip izdelka (6)', 'producttype_6', 42, 5, 1, 0, 0)") 
or die(mysqli_error($link));

Note: Add backticks ` for column names in your insert query as some of your column names are reserved words.

Wednesday, March 31, 2021
 
JustSteveKing
answered 8 Months ago
74

From the PHP Manual on mysqli_stmt::execute:

mysqli_stmt::execute -- mysqli_stmt_executeExecutes a prepared Query

Returns TRUE on success or FALSE on failure.


if ($stmt->execute()) { // exactly like this!
    $success = true;
}

You're doing it right... What's your dilemma?

Wednesday, March 31, 2021
 
Sauleil
answered 8 Months ago
47

Look at what you're asking:

if($stmt = mysqli_prepare($link, $sql)){
    // [...]
}

mysqli_stmt_close($stmt);

You always try to close your statement, even if it wasn't successfully created. You need to move that close attempt:

if($stmt = mysqli_prepare($link, $sql)){
    // [...]

    mysqli_stmt_close($stmt);
}

Additionally, the fact that you got this error in the first place means your SQL query failed to be prepared, so there's some kind of error in there. You are currently not checking for that, which can make life difficult for you if you want to figure out why your code isn't working. I recommend adding a check for that:

if($stmt = mysqli_prepare($link, $sql)){
    // [...]

    mysqli_stmt_close($stmt);
} else {
    echo "Something's wrong with the query: " . mysqli_error($link);
}

Note that in production code, you don't want to output the mysqli_error() directly since it may reveal information about your database that isn't anybody's business but yours, so only do this while you're debugging.

Thursday, August 19, 2021
 
Morrison Chang
answered 3 Months ago
46

First:
Why you have included the file wp-config.php? you don't need to include this at all.

Second:

You're accessing the instance of wpdb class ($wpdb). Which is in local scope.

You have to make $wpdb global:

global $wpdb;        // <--- making $wpdb as global
$wpdb->insert(
...
...
Thursday, September 2, 2021
 
Robert Balicki
answered 2 Months ago
92

A DataTable is raw rows and columns - Entity Framework works with .NET objects, which is fundamentally something else.

So you cannot just easily insert the rows and columns from a DataTable using EF.

You either need to iterate through your DataTable and build up objects from those rows & columns, stick those into a List<YourObject> and then persist those objects to the database using EF....

or you just skip EF and persist the "raw" DataTable to the database using raw ADO.NET (SqlDataAdapter or SqlCommand with an INSERT statement).

Update:

OK, so you want to convert your DataTable to objects. You need to have a class that represents your entity in the database - since you provided no information, I'm just going to call it MyObject

public class MyObject
{
   // define some properties
   public int ID { get; set; }
   public string Name { get; set; }
   // whatever else this object has
}

You most likely already have such an object - an entity class - that exists in your database.

Then you need to define a method to convert the data table to a list of objects:

public List<MyObject> ConvertDataTable(DataTable tbl)
{
     List<MyObject> results = new List<MyObject>();

     // iterate over your data table
     foreach(DataRow row in tbl.Rows)
     {
         MyObject convertedObject = ConvertRowToMyObject(row);
         results.Add(convertedObject);
     }

     return results;
} 

and now you need a last method to convert a single row to your object type:

public MyObject ConvertRowToMyObject(DataRow row)
{
     MyObject result = new MyObject();

     // assign the properties of MyObject from the DataRow
     result.ID = row.GetInt32(0);
     result.Name = row.GetString(1);
     // and so on .... convert the column values in the DataRow into the
     // properties of your object type

     return result;
}
Thursday, September 30, 2021
 
Jon
answered 1 Month ago
Jon
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :