-3

So $bays $wheelnumber and $lengthhours are set in a form and when the form is posted to here. But no matter what I do the variables wont write to the database. I've tried setting them directly in the code (i.e. $bays = '5') and that still wont work. But when i set ':number' => $_POST['wheelnumber'] it works!!!! also when I echo the variables the show up so i know they have been set. Someone please help. Its doing my head in!

    $bays = $_POST['bays'];
    $wheelnumber = $_POST['wheels'];
    $lengthhours = $_POST['length'];     

    //create user id
    $id = rand(1000000,99999990);

        if ($bays = ""){
        $bays = "1";}

        $length = $lengthours / $bays;

        if ($wheelnumber = ""){
        $wheelnumber = "1";}

    //create wheel number
    $name = "Wheel " . $wheelnumber;

    try {
        $connect = mysql_connect("localhost","root", "toor");
        if (!$connect) {
            die(mysql_error());
        }
        //insert into database with a prepared statement
        $stmt = $db->prepare('INSERT INTO wheels (name,number,id,stackid,length,farmid) VALUES (:name, :number, :id, :stackid, :length, :farmid)');
        $stmt->execute(array(
            ':name' => $name,
            ':number' => $wheelnumber,
            ':id' => $id,
            ':stackid' => $stack1,
            ':length' => $lengthhours,
            ':farmid' => $farmid,
        ));
    while ($bays > 0) {
                $bayname = "Bay " . $bays;
                $bid = rand(1000000,99999990);
        //insert into database with a prepared statement
        $stmt = $db->prepare('INSERT INTO bays (name,id,wheelid,length,farmid) VALUES (:name, :id, :wheelid, :length, :farmid)');
        $stmt->execute(array(
            ':name' => $bayname,
            ':id' => $bid,
            ':wheelid' => $id,
            ':length' => $length,
            ':farmid' => $farmid,
        ));
        $bays = $bays - 1;
}
noobie-php
  • 6,817
  • 15
  • 54
  • 101

2 Answers2

1

You say that $bays $wheelnumber and $lengthhours are set in a form but your code collected data from variables with different names i.e.

$bays = $_POST['bays'];
$wheelnumber = $_POST['wheels'];
$lengthhours = $_POST['length'];     

Should that be

$bays = $_POST['bays'];
$wheelnumber = $_POST['wheelnumber'];
$lengthhours = $_POST['lengthhours'];     

These if tests are wrong

if ($bays = ""){
    $bays = "1";
}

$length = $lengthours / $bays;

if ($wheelnumber = ""){
    $wheelnumber = "1";
}

The test for equals is == so change it to

if ($bays == ""){
    $bays = "1";
}

$length = $lengthours / $bays;

if ($wheelnumber == ""){
    $wheelnumber = "1";
}

You also seem to be connecting with the mysql_ database extension but are then trying to use the mysqli_ or maybe PDO database extension syntax. The 3 MySQL database extensions are not interchangable like that, pick one and stick to it.

i.e.

$connect = mysql_connect("localhost","root", "toor");

is using the mysql_ database extension, and

$stmt = $db->prepare('INSERT INTO wheels (name,number,id,stackid,length,farmid) VALUES (:name, :number, :id, :stackid, :length, :farmid)');

Is using the mysqli_ or maybe PDO database extension.

Also where does $db come from????

Also you seem to have coded a Try/Catch block but I am not sure if you coded the CATCH part, code is not complete so maybe I am wrong here

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
-2

Your line

$wheelnumber = $_POST['wheels'];

should be

$wheelnumber = $_POST['wheelnumber'];
foxbeefly
  • 510
  • 3
  • 13
  • 2
    *Is it an answer? Is it a question? Is it a comment? Who could say?* – al'ein Oct 07 '15 at 12:41
  • Hi Alan It is a proposed solution - pointing out a line of code from the question and suggesting a correction. – foxbeefly Oct 07 '15 at 12:44
  • 1
    It's a wondering. Ends with a question mark. Answers are like *Your problem **X** occurs because of **A, B** and **C**. To solve, do **Y**. This way, **D** and **E** will happen, solving your issue*. – al'ein Oct 07 '15 at 12:47
  • 1
    *Is it a bird? Is it a plane?* - No, [it's..............](http://www.youtube.com/watch?v=NWzROoxS2b4) @AlanMachado – Funk Forty Niner Oct 07 '15 at 13:17