After a database and its table(s) have been created, you can start adding data in them.
Some syntax rules:
INSERT INTO table_name VALUES (value1, value2, value3, ...)In this method, you must specify a value, even if it's NULL, for every column. If there are five columns in the table, you must list five values.
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)This method is generally preferable, you can add data only in the columns that matter. Any columns not given a value will be treated as NULL (or given a default value, if one was defined).
<?php // connect to the "tests" database $conn = new mysqli('localhost', 'root', 'pass', 'tests'); // check connection if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } // sql query for INSERT INTO users $sql = "INSERT INTO `users` (`name`, `pass`, `email`) VALUES ('Marius', 'faith', 'name@domain.net')"; // Performs the $sql query on the server to insert the values if ($conn->query($sql) === TRUE) { echo 'users entry saved successfully'; } else { echo 'Error: '. $conn->error; } $conn->close(); ?>- First, we create the mysqli object with the variable identifier $conn. Next, we build the SQL command string and save it to a variable called $sql. Then we call the query() method, and at the same time check its return value to determine if it was successful (true).
INSERT INTO table_name (column1, column2, column3,...) VALUES (valueA1, valueA2, valueA3,...), (valueB1, valueB2, valueB3,...), (valueC1, valueC2, valueC3,...)
<?php // connect to the "tests" database $conn = new mysqli('localhost', 'root', '', 'tests'); // check connection if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } // sql query for INSERT INTO users (two rows) $sql = "INSERT INTO `users` (`name`, `pass`, `email`) VALUES ('MarPlo', 'peace', 'user@domain.net'), ('I_AM', 'love', 'address@domain.net')"; // Performs the $sql query on the server to insert the values if ($conn->query($sql) === TRUE) { echo 'users entry saved successfully'; } else { echo 'Error: '. $conn->error; } $conn->close(); ?>
<?php // connect to the "tests" database $conn = new mysqli('localhost', 'root', 'pass', 'tests'); // check connection if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } // sql query for INSERT INTO users $sql = "INSERT INTO `users` (`name`, `pass`, `email`) VALUES ('PloMar ', 'love_light', 'a_name@domain.net')"; // Performs the $sql query and get the auto ID if ($conn->query($sql) === TRUE) { echo 'The auto ID is: '. $conn->insert_id; } else { echo 'Error: '. $conn->error; } $conn->close(); ?>Output:
If the table does't have a column with the AUTO_INCREMENT attribute or if the query wasn't an INSERT or UPDATE statement, the insert_id() will return zero.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>HTML form for insert users</title> </head> <body> <form action="insert.php" method="post"> Name: <input type="text" name="name" /><br /> Password: <input type="text" name="pass" /><br /> E-mail: <input type="text" name="email" /><br /> <input type="submit" value="Send" /> </form> </body> </html>When a user clicks on the "Send" button, the form data is sent to "insert.php" file.
<?php $erors = array(); // set an empty array that will contains the errors $regexp_mail = '/^([a-zA-Z0-9]+[a-zA-Z0-9._%-]*@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4})$/'; // an e-mail address pattern // Check for form submission if (isset($_POST['name']) && isset($_POST['pass']) && isset($_POST['email'])) { // remove tags and whitespace from the beginning and end of form data $_POST = array_map("strip_tags", $_POST); $_POST = array_map("trim", $_POST); // chech if all form fields are filled in correctly // (email address and the minimum number of characters in "name" and "pass") if (!preg_match($regexp_mail, $_POST['email'])) $erors[] = 'Invalid e-mail address'; if (strlen($_POST['name'])<3) $erors[] = 'Name must contain minimum 3 characters'; if (strlen($_POST['pass'])<6) $erors[] = 'Password must contain minimum 6 characters'; // if no errors ($error array empty) if(count($erors)<1) { // connect to the "tests" database $conn = new mysqli('localhost', 'root', '', 'tests'); // check connection if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } // store the values in an Array, escaping special characters for use in the SQL statement $adds['name'] = $conn->real_escape_string($_POST['name']); $adds['pass'] = $conn->real_escape_string($_POST['pass']); $adds['email'] = $conn->real_escape_string($_POST['email']); // sql query for INSERT INTO users $sql = "INSERT INTO `users` (`name`, `pass`, `email`) VALUES ('". $adds['name']. "', '". $adds['pass']. "', '". $adds['email']. "')"; // Performs the $sql query on the server to insert the values if ($conn->query($sql) === TRUE) { echo 'users entry saved successfully'; } else { echo 'Error: '. $conn->error; } $conn->close(); } else { // else, if errors, it adds them in string format and print it echo implode('<br />', $erors); } } else { echo 'No data from form'; } ?>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net