Witam.
Od razu zacznę od kodu:
<?php
class showClass {
public $mysqli;
public $err = '';
public function __construct()
{
$this->mysqli = new mysqli("", "", "", "");
if ($this->mysqli->connect_errno) {
$this->err = "Blad polaczenia: " . $this->mysqli->connect_error;
}
}
public function show() {
$select = "SELECT * FROM test_info";
$result = $this->mysqli->query($select);
while ($newArray = $result->fetch_array()) {
$resultArray[] = $newArray;
}
return $resultArray;
}
public function insert($fields)
{
$insert = "INSERT INTO test_info ('name', 'email') values ($fields)";
$this->mysqli->query($insert);
}
}
$records = new showClass();
$i=0;
foreach($records->show() as $key => $value) {
$i++;
echo "{$i}. {$value['name']} ({$value['email']}) <br>"; }
if(!isset($_POST['submit'])) {
?>
<form action="index.php" method="POST">
<input type="text" id="name" name="name"><br>
<input type="text" id="email" name="email"><br>
<button type="submit" name="submit">Add record</button>
</form>
<?php
} else {
$name = $_POST['name'];
$email = $_POST['email'];
$fields = "'" . $name . "', '" . $email . "'";
$records->insert($fields);
}
Działa mi to, ale nie do końca...
Niby wysyła, ale do bazy nie dodaje danych. var_dump() mi pokazuje iż zapytanie w metodzie jest poprawne, ale już samo query() jakby się nie wywołuje.
Ucze się PHP OOP i nie mam pomysłu na to teraz.
Prosze o jakiś feedback (IMG:
style_emoticons/default/smile.gif)
Pozdrawiam!
Ten post edytował Bureau 10.10.2013, 18:02:43