문제

Hi All I have created a form below (entryform.php)to enter order details. form contains: part_id, price, model. I already created connect.php to connect to the database, and custompcorder.php to insert data in order_details table and entryform.php to enter data.

Here is what I will like to do on my entryform.php: 1- make the Part input field as A drop down that get part name and part_id from Parts table value to save is part_id but show part name on the entryform. 2- After the part is picked in the drop down, I want the fields price and model to be automatically filled with appropriate data from part table. Autofill will use the part_id to get the correct data. as you can see on the form below, many rows will be entered at a time.

  entryform.php

<?php

<form action="../action/custompcorder.php/" method="post">
<p>Part: <input type="text" name="part_id"/> Price: <input type="text" name="price"/> Model: <input type="text" name="model"/></p> 
<p>Part: <input type="text" name="part_id2"/> Price: <input type="text" name="price2"/> Model: <input type="text" name="model2"/></p> 
<input type="submit" value="Submit" /></form>

?>



connect.php

<?php

$dbhost = 'localhost';
$dbuser = 'user2';
$dbpass = 'password';
$dbname = 'db3';

$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
// Check connection
if (mysqli_connect_errno())
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>


custompcorder.php

<?php
    include_once '../action/connect.php';

$sql="INSERT INTO order_details (part_id, price,model)
VALUES
('$_POST[part_id]','$_POST[price]','$_POST[model]'),
('$_POST[part_id2]','$_POST[price2]','$_POST[model2]')";


?>

올바른 솔루션이 없습니다

다른 팁

First of all, your input fields are text, so it doesn't make sense there. I would write a function for it, so you can simply change the query within it. As an example below here.

Editted after questio in comments

Have you checked with the development tools in google chrome that page? it shows you the lay-out and CSS (part 1, the mark up). and how it's build up. From there on, you can check with what functions you need to use. In your case you need a couple of dropdowns and you need to do some database work.

For instance, say you want to show the RAM memory, and you have a database called models, make sure you have a partId, partPrice, partName and partType. For instance, if you have RAM, and it's a RAM memory part, say you assign the value 1 to it. In your case your query of the dropdown would be (as I explained above)

function LoadRAMParts(){
    $query = "SELECT *
        FROM models
        WHERE partType = '1';
        ORDER BY partName DESC;";
    $resultaat=mysqli_query($query);
    return $resultaat;
}

So you get returned all your RAM Memory parts now, which you can now just simply load into your dropdown menu by using a bit of PHP

function MakeRAMMemoryDropDown(){  // define name of the php function and open the function
echo "<select name='RAMPartId' class='dropdown'>"; // open the dropdown with the name RAMPPartId
    $results = LoadRAMParts(); // Load in the query from earlier
    while($row = mysqli_fetch_array($results)){   // while lets it loop through your results as long as there are results
    echo "<option value='".$row['partId']."'>" // the value for the ID when it's chosen
    .$row['partName']."</option>"; // the name of the product the user sees
    } // end of the loop
echo "</select>"; // end of the dropdown
} // end of the php function

This you exectue in a seperate file that you include on the top. (I run first my queries first, then create the dropdowns, else it becomes a conflict, because the function i call within my dropdown function has to excist, prior to the dropdown. For me it looks like this.

enter image description here

So as final part, and I hope i explained it to you a bit, you need to call in the function into your PHP page where you want to use it. That just goes simply by the code:

<?php  MakeRAMMemoryDropDown() ?>

As example:

enter image description here

And thats called dynamicly loading in your content. Now I use a different type of code placement and different ideas of generating contenct, but to each programmer it's own style. This is how I load in my content.

As for your loading prices question, you have to that with AJAX (Asynchronous JavaScript And XML) but thats not my cup of tea, I cant help you there. Sorry.

I hope I explained it to you, if not, I hope you figure it out another way then.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top