0

The script worked for roughly 6 months but then suddenly stopped writing to one of the 3 tables in MySQL. Script writes to the client_license and clients_notes tables but wont for the life of it write to the clients table. As I say it did work perfectly up until 2 weeks ago. Nothing changed to the code and I can't seem to find any issues on the DB.

<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
    header('Location: index.html');
    exit();
}
if( $_SESSION['access'] != "admin") {
    session_destroy();
    header("location: index.html");
}
?>
<?php
$DATABASE_HOST = 'localhost';
$DATABASE_USER = '***';
$DATABASE_PASS = '***';
$DATABASE_NAME = '***';
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if (!isset($_POST['clients_initials'], $_POST['clients_name'], $_POST['clients_nickname'], $_POST['clients_surname'], $_POST['clients_id_nr'], $_POST['clients_profession'], $_POST['clients_tel'], $_POST['clients_tel_alt'], $_POST['clients_email'], $_POST['clients_address_complex'], $_POST['clients_address'], $_POST['clients_suburb'], $_POST['clients_town'], $_POST['clients_province'], $_POST['client_license_nr'], $_POST['client_license_expiry'], $_POST['client_license_code'])) {
    header("Location: statusclient1a_admin.php");
    die();
}
if ($stmt = $con->prepare('SELECT * FROM clients WHERE clients_id_nr = ?')) {
    $stmt->bind_param('s', $_POST['clients_id_nr']);
    $stmt->execute();
    $stmt->store_result();
    if ($stmt->num_rows > 0) {
    header("Location: statusclient2a_admin.php");
    die();
    } else 
    {
if ($stmt = $con->prepare('INSERT INTO clients (clients_initials, clients_name, clients_nickname, clients_surname, clients_id_nr, clients_profession, clients_address, clients_suburb, clients_town, clients_province, clients_tel, clients_email, clients_tel_alt, clients_address_complex, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')) {
    $stmt->bind_param('sssssssssssssss',$_POST['clients_initials'], $_POST['clients_name'], $_POST['clients_nickname'], $_POST['clients_surname'], $_POST['clients_id_nr'], $_POST['clients_profession'], $_POST['clients_address'], $_POST['clients_suburb'], $_POST['clients_town'], $_POST['clients_province'], $_POST['clients_tel'], $_POST['clients_email'], $_POST['clients_tel_alt'], $_POST['clients_address_complex'], $_SESSION['user_id']);
    $stmt->execute();
    $GET_last_ID = mysqli_insert_id($con);
    $url = 'addvehicle_admin.php?id=' . $GET_last_ID;
        header("Location: $url");
} else {
    header("Location: statusclient3a_admin.php");
    die();
}
    }
    if ($stmt1 = $con->prepare('INSERT INTO client_license (clients_id, client_license_nr, client_license_expiry, client_license_code, user_id) VALUES (?, ?, ?, ?, ?)')) {
    $stmt1->bind_param('sssss',$GET_last_ID, $_POST['client_license_nr'], $_POST['client_license_expiry'], $_POST['client_license_code'], $_SESSION['user_id']);
    $stmt1->execute();
    $stmt1->close();
    }
    if ($stmt2 = $con->prepare('INSERT INTO client_notes (clients_id, client_notes_data, user_id) VALUES (?, ?, ?)')) {
    $stmt2->bind_param('sss',$GET_last_ID, $created, $_SESSION['user_id']);
    $created = "Client added to database";
    $stmt2->execute();
    $stmt2->close();
    }   
} else {
    header("Location: statusclient4a_admin.php");
    die();
}
$con->close();
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Wouter
  • 11
  • 1
  • 2
    You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Jul 26 '21 at 12:52
  • 1
    Check your server error log. – Grumpy Jul 26 '21 at 12:52
  • You need to be more clear what happens when you say it won't write to the table. Does the script crash? Do you see any output? Is any part of this script executed? Also, please create [mcve]. We don't need to see all of your code, we only need to see the reproducible scenario – Dharman Jul 26 '21 at 12:58
  • 1
    "won't" isn't an error message, or a useful description of the behaviour of the code. We need to know what happens instead of what you expected. More than likely, debuggging and proper error checking will reveal it. So in addition to the links mentioned above, I also advise you to study http://www.phpknowhow.com/basics/basic-debugging/ so you can start to debug your code and narrow down the issue (which is especially useful if you don't encounter any useful error messages after you've properly enabled the logging functionality). – ADyson Jul 26 '21 at 13:12
  • Also, if you're saying it worked until recently but you're certain you haven't changed the code, then a) re-test your code in your development environment to try and verify that theory, and b) look for other environmental changes which may have occurred (even if you didn't initiate them) such as PHP or mysql upgrades / changes, different type of data being provided, changes to database structure, PHP / Apache /Mysql config changes, etc - basically anything which could be relevant to the issue. This will be easier once you have a more accurate problem report (e.g. an error message). – ADyson Jul 26 '21 at 13:14
  • If you haven't changed the code then perhaps something about the table definition has changed e.g. an additional column that doesn't allow nulls or a change of datatype? – Dazz Knowles Jul 26 '21 at 13:16
  • 1
    Hi All, 1st of all my apologies for the late reply. 2ndly thank you for all your replies. It was insightful. I did manage to resolve the problem I had. There is a couple of fields in the database which contain extra info that are not necessarily filled with data. I had to change these fields to "can be NULL" which resolved my issue. Worked fine before so this still tickles me a bit. Anyway thanks again for all your responses – Wouter Aug 13 '21 at 09:25

0 Answers0