If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Entering data into Mysql table

Started by mishraviplav7877, Oct 17, 2022, 11:11 AM

Previous topic - Next topic

mishraviplav7877Topic starter

Hello everyone!

I am a beginner in PHP and have already created a connection to the database, as well as a simple form which can be used to add data into one table. However, I was wondering how to create a drop-down menu with table names, so that the user can select a table before entering data into it.

In regards to this topic, here is some sample code for the form.php file which connects to a database and enters data into a table:

<?php
$dsn 
"mysql:host=localhost;dbname=new_db;charset=utf8";
$options = [
       
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
       
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
     
];
$pdo = new PDO($dsn'blabla''blabla'$options);
$name $_POST['name'];
$contacts $_POST['contacts'];
$problem $_POST['problem'];
$data $_POST['data'];
$stmt $pdo->prepare("INSERT INTO users (name, contacts, problem, data) VALUES (:name, :contacts, :problem, :data)");
$stmt->bindParam(':name'$name);
$stmt->bindParam(':contacts'$contacts);
$stmt->bindParam(':problem'$problem);
$stmt->bindParam(':data'$data);
$falg $stmt->execute();
//$mail_to = 'blabla@mail.ru';
//mail ($mail_to, "New ticket", "$name, $problem");
?>

And here is some sample code for the index.php file which contains the form itself:

<body>
<form action="form.php" method="post" name="forma">
<fieldset>
<label for="name">Shop name:</label><br/>
<input type="text" name="name" size="200"><br/>
<label for="contacts">Contact details:</label><br/>
<input type="text" name="contacts" size="200"><br/>
<label for="problem">Problem description:</label><br/>
<input type="text" name="problem" size="200"><br/>
<label for="data">Current date:</label><br/>
<input name="data" type="datetime-local">
</fieldset>
<br/>
<fieldset>
<input id="submit" type="submit" value="Submit data"><br/>
</fieldset>
</form>
</body>
</html>
  •  

Fan_Tema

If you find yourself in a situation where the fields are the same, but the tables are different, it is likely that you are doing something wrong. If this is not the case, start by selecting a table using a simple link before displaying the form with the appropriate fields. Alternatively, you can obtain a list of fields from an empty selection for an empty form. The metadata of the field can be acquired using mysqli_result::fetch_field in mysqli or PDOStatement::getColumnMeta in PDO.

It is also possible to use a separate table to store information about the tables being edited. However, instead of having a "table of tables" you can use a table of stores and add a store ID field to the application table. Then, form the drop-down list according to the store table and pass the store ID from the form rather than the name.

ID verification in the form handler can be done either explicitly, through a separate request, or by means of the DBMS itself (see "support for foreign keys in MySQL").

In general, it is not advisable to duplicate the same type of tables, except in rare cases. Can you provide more information about the type of store you are working with - is it a brick-and-mortar store or an online store?
  •  

tookempe

To create a drop-down menu with table names to allow the user to select a table before entering data, you can modify the `form.php` and `index.php` as follows:

In the `index.php` file, you can add a drop-down menu for table selection below the form fields. To do this, modify the code as shown below:

<body>
<form action="form.php" method="post" name="forma">
<fieldset>
<label for="table">Select Table:</label><br/>
<select name="table">
  <option value="users">Users</option>
  <option value="other_table_name">Other Table Name</option>
  <!-- Add more options for other tables -->
</select><br/>

<label for="name">Shop name:</label><br/>
<input type="text" name="name" size="200"><br/>
<label for="contacts">Contact details:</label><br/>
<input type="text" name="contacts" size="200"><br/>
<label for="problem">Problem description:</label><br/>
<input type="text" name="problem" size="200"><br/>
<label for="data">Current date:</label><br/>
<input name="data" type="datetime-local">
</fieldset>
<br/>
<fieldset>
<input id="submit" type="submit" value="Submit data"><br/>
</fieldset>
</form>
</body>
</html>


In the `form.php` file, you can modify the PHP code to handle the selected table from the drop-down menu and insert data into the corresponding table. Modify the code as shown below:

<?php
$dsn = "mysql:host=localhost;dbname=new_db;charset=utf8";
$options = [
       PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
       PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
     ];
$pdo = new PDO($dsn, 'blabla', 'blabla', $options);
$table = $_POST['table'];
$name = $_POST['name'];
$contacts = $_POST['contacts'];
$problem = $_POST['problem'];
$data = $_POST['data'];

$stmt = $pdo->prepare("INSERT INTO $table (name, contacts, problem, data) VALUES (:name, :contacts, :problem, :data)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':contacts', $contacts);
$stmt->bindParam(':problem', $problem);
$stmt->bindParam(':data', $data);
$falg = $stmt->execute();
//$mail_to = 'blabla@mail.ru';
//mail ($mail_to, "New ticket", "$name, $problem");
?>


By making these modifications, you will be able to provide a drop-down menu for the user to select a specific table before entering data, and the PHP code will insert the data into the selected table accordingly.


To complete the functionality, you can modify the `form.php` and `index.php` files as follows:

In the `index.php` file, you can add a drop-down menu for table selection below the form fields. To do this, modify the code as shown below:

<body>
<form action="form.php" method="post" name="forma">
<fieldset>
<label for="table">Select Table:</label><br/>
<select name="table">
  <option value="users">Users</option>
  <option value="orders">Orders</option>
  <!-- Add options for other tables -->
</select><br/>

<label for="name">Shop name:</label><br/>
<input type="text" name="name" size="200"><br/>
<label for="contacts">Contact details:</label><br/>
<input type="text" name="contacts" size="200"><br/>
<label for="problem">Problem description:</label><br/>
<input type="text" name="problem" size="200"><br/>
<label for="data">Current date:</label><br/>
<input name="data" type="datetime-local">
</fieldset>
<br/>
<fieldset>
<input id="submit" type="submit" value="Submit data"><br/>
</fieldset>
</form>
</body>
</html>


In the `form.php` file, you can modify the PHP code to handle the selected table from the drop-down menu and insert data into the corresponding table. Modify the code as shown below:

<?php
$dsn = "mysql:host=localhost;dbname=new_db;charset=utf8";
$options = [
       PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
       PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
     ];
$pdo = new PDO($dsn, 'blabla', 'blabla', $options);
$table = $_POST['table'];
$name = $_POST['name'];
$contacts = $_POST['contacts'];
$problem = $_POST['problem'];
$data = $_POST['data'];

$stmt = $pdo->prepare("INSERT INTO $table (name, contacts, problem, data) VALUES (:name, :contacts, :problem, :data)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':contacts', $contacts);
$stmt->bindParam(':problem', $problem);
$stmt->bindParam(':data', $data);
$falg = $stmt->execute();
//$mail_to = 'blabla@mail.ru';
//mail ($mail_to, "New ticket", "$name, $problem");
?>


By making these modifications, you will be able to provide a drop-down menu for the user to select a specific table before entering data, and the PHP code will insert the data into the selected table accordingly. This will enhance the functionality of your form by allowing users to choose the table into which they want to insert data.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...