skip to Main Content

MySQL is not using the variables as it should. it is not taking any value from them it is incrementing the auto-increment numbers in the MYSQL table, however the row is not saved. I am not given any errors.

I have tried like this:

$sql = "INSERT INTO `tbl_bike` (`userID`, `ManuPartNo`, `BikeManufacturer`, `BikeModel`, `BikeType`, `BikeWheel`, `BikeColour`, `BikeSpeed`, `BrakeType`, `FrameGender`, `AgeGroup`, `DistFeatures`) 
VALUES (“.$userID.”, “.$PartNo.”, “.$BikeManufacturer.”, “.$BikeModel.”, “.$BikeType.”, “.$BikeWheel.”, “.$BikeColour.”, “.$BikeSpeed.”, “.$BrakeType.”, “.$FrameGender.”, “.$AgeGroup.”, “.$DistFeatures.”)";

I have also tried replacing the ” with ‘, Removing the . and even completely removing the “. Nothing has helped with this issue. When I use this query but remove the variables and instead put string, int etc in the correct places the query will function perfectly and put the results into the table. My variables are normally as follows:

$PartNo =  $_POST['ManuPartNo’];

$BikeManufacturer =  $_POST['BikeManufacturer’]; 

$BikeModel =  $_POST['BikeModel’]; 

$BikeType =  $_POST['BikeType’]; 

$BikeWheel =  $_POST['BikeWheel’]; 

$BikeColour =  $_POST['BikeColour’]; 

$BikeSpeed =  $_POST['BikeSpeed’]; 

$BrakeType =  $_POST['BrakeType’]; 

$FrameGender =  $_POST['FrameGender’]; 

$AgeGroup =  $_POST['AgeGroup’]; 

$DistFeatures =  $_POST['DistFeatures’]; 


These variables normally take input from a separate PHP/HTML file with the '$_POST['DistFeatures’];'

I have tried removing the $_POST['DistFeatures’]; from the ends of each of them and just replacing the values with normal string or int values but still nothing helps. I am completely stuck and would appreciate any help with this.

This is all running on a plesk server.

2

Answers


  1. Please stop using deprecated MySQL. I will suggest an answer using PDO. You can use this to frame your other queries using PDO.

    // Establish a connection in db.php (or your connection file)
    $dbname = "dbname"; // your database name
    $username = "root"; // your database username
    $password = ""; // your database password or leave blank if none
    $dbhost = "localhost";
    $dbport = "10832";
    
    $dsn = "mysql:dbname=$dbname;host=$dbhost";
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    
    // Include db.php on every page where queries are executed and perform queries the following way
    
    // Take Inputs this way (your method is obsolete and will return "Undefined Index" error)
    
    $userId = (!empty($_SESSION['sessionname']))?$_SESSION['sessionname']:null; // If session is empty it will be set to Null else the session value will be set
    $PartNo = (!empty($_POST['ManuPartNo']))?$_POST['ManuPartNo']:null; // If post value is empty it will be set to Null else the posted value will be set
    $BikeManufacturer = (!empty($_POST['BikeManufacturer']))?$_POST['BikeManufacturer']:null;
    $BikeModel = (!empty($_POST['BikeModel']))?$_POST['BikeModel']:null;
    $BikeType = (!empty($_POST['BikeType']))?$_POST['BikeType']:null;
    $BikeWheel = (!empty($_POST['BikeWheel']))?$_POST['BikeWheel']:null;
    
    // Query like this
    $stmt = $pdo->prepare("INSERT INTO(`userID`, `ManuPartNo`, `BikeManufacturer`, `BikeModel`, `BikeType`)VALUES(:uid, :manuptno, :bkman, :bkmodel, :bktype)");
    $stmt-> bindValue(':uid', $userId);
    $stmt-> bindValue(':manuptno', $PartNo);
    $stmt-> bindValue(':bkman', $BikeManufacturer);
    $stmt-> bindValue(':bkmodel', $BikeModel);
    $stmt-> bindValue(':bktype', $BikeType);
    $stmt-> execute();
    
    if($stmt){
        echo "Row inserted";
    }else{
        echo "Error!";
    }
    

    See, it’s that simple. Use PDO from now on. It’s more secured. To try this, just copy the whole code in a blank PHP file and and run it. Your database will receive an entry. Make sure to change your database values here.

    Login or Signup to reply.
  2. You should try this

    $sql = "INSERT INTO tbl_bike (userID, ManuPartNo, BikeManufacturer, BikeModel, BikeType, BikeWheel, BikeColour, BikeSpeed, BrakeType, FrameGender, AgeGroup, DistFeatures) VALUES ('$userID', '$PartNo', '$BikeManufacturer', '$BikeModel', '$BikeType', '$BikeWheel', '$BikeColour', '$BikeSpeed', '$BrakeType', '$FrameGender', '$AgeGroup', '$DistFeatures')";
    

    If this doesn’t work, enable the null property in sql values. So you can find out where the error originated.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search