« Отговор #6 -: Jul 17, 2017, 06:08 »
Ето формата:
addclient.php:
---------------
<form action="action.php" method="post">
<p>Име на клиент: <input type="text" name="client_name" /></p>
<p>Адрес:<input type="text" name="client_addr" /></p>
<p>Телефон:<input type="text" name="client_tel" /></p>
<p>БУЛСТАТ:<input type="text" name="client_bulstat" /></p>
<p>ИД. Номер<input type="text" name="client_idnum" /></p>
<p>МОЛ:<input type="text" name="client_mol" /></p>
<p>Получател:<input type="text" name="client_get" /></p>
<p><input type="submit" /></p>
</form>
----------
sqlite3 вариант на action.php:
----------
<?php
$client_name=htmlspecialchars($_POST['client_name']);
$client_addr=htmlspecialchars($_POST['client_addr']);
$client_tel=htmlspecialchars($_POST['client_tel']);
$client_bulstat=htmlspecialchars($_POST['client_bulstat']);
$client_idnum=htmlspecialchars($_POST['client_idnum']);
$client_mol=htmlspecialchars($_POST['client_mol']);
$client_get=htmlspecialchars($_POST['client_get']);
$clientsdb = new SQLite3('clientsdb.db') or die('Unable to open database');
$clientsdb->exec("INSERT INTO clients (
client_name,
client_addr,
client_tel,
client_bulstat,
client_idnum,
client_mol,
client_get
)
VALUES (
'$client_name',
'$client_addr',
'$client_tel',
'$client_bulstat',
'$client_idnum',
'$client_mol',
'$client_get')"
) or die('Create db failed');
$clientsdb->close();
?>
--------
PDO вариант на action.php (вчера един познат ми помага за него, преди да пиша във форума, той твърди, че при него прави само 1 ред, а при мен продължава да прави два. Почти съм сигурен, че той задава променливите статично, а не през _POST, когато тества, което ме кара да мисля, че проблема е при задаването на променливите..)
–––––
<?php
$client_name=htmlspecialchars($_POST['client_name']);
$client_addr=htmlspecialchars($_POST['client_addr']);
$client_tel=htmlspecialchars($_POST['client_tel']);
$client_bulstat=htmlspecialchars($_POST['client_bulstat']);
$client_idnum=htmlspecialchars($_POST['client_idnum']);
$client_mol=htmlspecialchars($_POST['client_mol']);
$client_get=htmlspecialchars($_POST['client_get']);
$clientsdb = new PDO('sqlite:clientsDB.db');
$clientsdb->exec("CREATE TABLE IF NOT EXISTS clients (
client_id INTEGER PRIMARY KEY,
client_name TEXT,
client_addr TEXT,
client_tel TEXT,
client_bulstat TEXT,
client_idnum TEXT,
client_mol TEXT,
client_get TEXT)");
$stmt = $clientsdb->query("SELECT name
FROM sqlite_master
WHERE type = 'table'
ORDER BY name");
/* check if tables are created
$tables = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$tables[] = $row['name'];
}
foreach($tables as $table)
{
print_r($table);
}
*/
$insert = "INSERT INTO clients (client_name, client_addr, client_tel, client_bulstat, client_idnum, client_mol, client_get)
VALUES (:client_name, :client_addr, :client_tel, :client_bulstat, :client_idnum, :client_mol, :client_get)";
$stmt = $clientsdb->prepare($insert);
$stmt->bindParam(':client_name', $client_name);
$stmt->bindParam(':client_addr', $client_addr);
$stmt->bindParam(':client_tel', $client_tel);
$stmt->bindParam(':client_bulstat', $client_bulstat);
$stmt->bindParam(':client_idnum', $client_idnum);
$stmt->bindParam(':client_mol', $client_mol);
$stmt->bindParam(':client_get', $client_get);
$stmt->execute();
echo $clientsdb->lastInsertId();
?>
П.П. - mystical мерси за отговорите, довечера продължавам да го мъча.