0

Hello i have a problem with my php where only the last value in the rows is inserted in the mysql database:Last value inserted from rows

mysql database only show the last value to be inserted from insert record

I want the three of the data to be inserted not only the last values... How do i do that?

here is my insert record behavior code:

 if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
 $insertSQL = sprintf("INSERT INTO ordering (productCode, Name, paymentMethod, Quantity, TotalPrice) VALUES (%s, %s, %s, %s, %s)",
                   GetSQLValueString($_POST['productcode'], "int"),
                   GetSQLValueString($_POST['Name'], "text"),
                   GetSQLValueString($_POST['PaymentMethod'], "text"),
                   GetSQLValueString($_POST['quantity'], "int"),
                   GetSQLValueString($_POST['totalprice'], "double"));

 mysql_select_db($database_perfume_connection, $perfume_connection);
 $Result1 = mysql_query($insertSQL, $perfume_connection) or die(mysql_error());

 $insertGoTo = "member_perfume_homepage.php";
 if (isset($_SERVER['QUERY_STRING'])) {
 $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
 $insertGoTo .= $_SERVER['QUERY_STRING'];
 }
 header(sprintf("Location: %s", $insertGoTo));
}

2 Answers 2

1

Please show your html page from where you are sending post data. I think you should have to make an array of $_POST variables then you can get all the records at php side and you can insert all three records in table.

Try this

Please check the below link where you can find your solution Inserting Multiple Rows with PHP & MySQL

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

2 Comments

hey, thank you for answering... I dont know how exactly to show html page... since im not so good at php because im mainly just doing this for school projects... can u show me how?i really ni to get this done before tuesday
I tried that but i got so many errors.. + i dont understand that solution though in which to put where the coding is..
0

Create Model that holds all table columns.

class OrderModel  extends BaseModel{

    public $id;
// Fill here all columns


    public function __construct($data) {
        foreach ($data as $key => $value) {
            $this->$key = $value;
        }
    }
public function get_table_name() {
return "ordering";
}
}

Create Base model class and put insert method in this class.

public function insert() {
        $dbh = DB::connect();
        $object_keys = array_keys(get_object_vars($this));
        $query = "INSERT INTO " . $this->get_table_name() . " (" . implode(',', $object_keys) . ") VALUES(:" . implode(',:', $object_keys) . ");";
        $sth = $dbh->prepare($query);
        foreach ($object_keys as $key) {
            $sth->bindParam(':' . $key, $this->$key);
        }
        $sth->execute();
        $this->id = $dbh->lastInsertId();
        return TRUE;
    }
$filtered_post = filter_input_array(INPUT_POST);
$order = New OrderModel($filtered_post);
$order->insert();
  • Use PDO instead mysql_ functions
  • Use Filter functions to filter POST Array

1 Comment

Hey thank you for answering appreciate it... So what i nid to do?i copy back all of the codings and insert it in the php? or what?

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.