user3713946 user3713946 - 29 days ago 7
MySQL Question

How can I send two querys for different tables in PHP?

I want to create new data in the talbe named "preise". If i do that, I want to add a column in a table named "tische" with the title of the data i have created in "preise"
I hope you can understand what I mean (i'm not very good in English")
This is my PHP:

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){

//Getting values
$name = $_POST['name'];
$ebene = $_POST['ebene'];
$preis = $_POST['preis'];

//Creating an sql query
$sql = "INSERT INTO preise (name,ebene,preis) VALUES ('$name','$ebene','$preis')";
$add = "ALTER TABLE tische ADD COLUMN $name INT(5) NOT NULL AFTER id";

//Importing our db connection script
require_once('dbConnect.php');

//Executing query to database
if(mysqli_query($con,$sql,$add)){
echo 'Element erfolgreich hinzugefügt';
}else{
echo 'Konnte Element nicht hinzufügen';
}

//Closing the database
mysqli_close($con);
}


If I delete the line at $add... then the script will execute the $sql as it should. But how can I execute the $add query?

Answer

Different queries should be executed separately.

A simple example:

//Creating an sql query
$insert_query = "INSERT INTO preise (name,ebene,preis) VALUES ('$name','$ebene','$preis')";
$alter_query = "ALTER TABLE tische ADD COLUMN $name INT(5) NOT NULL AFTER id";

//Importing our db connection script
require_once('dbConnect.php');

//Executing query to database

// mysqli_query takes TWO or THREE arguments only
// and third argument is an INT value
// so passing another query as third argument is USELESS
if (mysqli_query($con, $insert_query)){

    // if first query is okay
    // we can run second one
    $r = mysqli_query($con, $alter_query);
    if ($r) {
        // both queries are OK
        echo 'Element erfolgreich hinzugefügt';
    } else {
        echo 'Some error message here';
    }
} else {
    echo 'Konnte Element nicht hinzufügen';
}

//Closing the database 
mysqli_close($con);

For further readings:

Comments