Nearly every site that you visit nowadays has some sort of database storage in place, many sites opt to use MySQL databases when using PHP. However, many people haven’t yet taken the step to interacting with databases properly in PHP. Here we guide you through what you should be doing – using PHP’s MySQLi class – with a hat-tip to the one way that you definitely shouldn’t be doing it.
Insert query & move uploaded file in Mysqli
database connection is must under function for this query if using classes.
DB Connection:
1 2 3 4 5 6 7 8 9 | <?php $db = new mysqli('localhost', 'root', '', 'database'); if($db->connect_errno > 0){ die('Unable to connect to database [' . $db->connect_error . ']'); } ?> |
INSERT Query:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php $banner=$_FILES['banner']; $bannername1=urlencode($banner['name']); $bannername2=str_replace("+", "_", $bannername1); $bannername=str_replace("-", "_", $bannername2); $code=time(); $position="0"; $status="on"; $link=urlencode($_POST['link']); $des="none"; $insert=mysqli_query($db,"INSERT INTO TableName (`banner`,`des`,`position`,`status`,`link`) VALUES ('$code$bannername','$des','$position','$status','$link')") or die(mysqli_error($db)); move_uploaded_file($banner['tmp_name'], "../images/slider/$code$bannername"); ?> |