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.


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 but it's not helping me much.



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
answered 8 Months ago

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
answered 8 Months ago

Look at what you're asking:

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


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)){
    // [...]


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)){
    // [...]

} 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

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


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
Thursday, September 2, 2021
Robert Balicki
answered 2 Months ago

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).


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);

     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
answered 1 Month ago
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 :