0

I try to pass some data to mysql but i didnt work.did i miss something. i already check my database name and table.

<?php
$dbServername = "localhost";
$dbUsername   = "root";
$dbPassword   = "";
$dbName       = "pelanggan";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
if (!$conn) {
    die('error connecting to database');
}

It keeps giving me the "connection error" message:

<?php
include_once 'includes/dbh.inc.php';
?>
    <html>
    <body>
    <form action='form.php' method='POST'>
        NAMA: <br> <input type='text' name='nama'><br>
        NO TELP : <br> <input type='text' name='telp'><br>
        PAKET DATA: <br> <input type='text' name='paket'><br>
        <button value='submit' name='submit'>submit</button>
    </form>
    </body>
    </html>
<?php
if (isset($_POST['nama']) && isset ($_POST['telp']) && isset($_POST['paket'])) {
    $nama  = $_POST['nama'];
    $telp  = $_POST['telp'];
    $paket = $_POST['paket'];
    if (empty ($nama) || empty ($telp) || empty ($paket)) {
        echo '*FIELDS MUST BE FILLED';
    } else {
        $sqlinsert = "INSERT INTO daftarpelanggan (nama,telp,paket) VALUES ('$nama','$telp','$paket')";
        if (!mysqli_query($conn, $sqlinsert)) {
            die ('connection error');
        } else {
            echo '1 record has been added successfully';
        }
    }
}
?>

"Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Table 'daftarpelanggan' is read only

12
  • 2
    Try using die ('connection error' . mysqli_error($conn)); and tell what you get. Commented May 10, 2018 at 14:50
  • 2
    Your code is vulnerable to SQL Injection by the way. Make sure to sanitise your user input before executing a SQL query with it embedded within. Anyway, do post the results from the query @Praveen Kumar posted as it will give us some more detailed information. Commented May 10, 2018 at 14:53
  • Add ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. Commented May 10, 2018 at 15:01
  • @Praveen Kumar it says "connection errorTable 'daftarpelanggan' is read only" Commented May 10, 2018 at 15:03
  • @RiggsFolly i did tried you suggestion and it says '"Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Table 'daftarpelanggan' is read only' in C:\xampp\htdocs\coba\form.php:22 Stack trace: #0 C:\xampp\htdocs\coba\form.php(22): mysqli_query(Object(mysqli), 'INSERT INTO daf...') #1 {main} thrown in C:\xampp\htdocs\coba\form.php on line 22"' Commented May 10, 2018 at 15:10

1 Answer 1

1

If connection to db gives error your queries won't evaluate. try to get what error it gives using mysqli_connect_error() function.

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
//if (!$conn) {
//    die('error connecting to database');
//}
if (mysqli_connect_errno()){ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Solve the error based on the error message.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually there is nothing wrong with the concatenation. Remember $variables are automatically expanded in a double quoted string literal in PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.