这是我的代码,这仅用于学校作业,我知道它很容易被sql注入。我需要if语句来创建sql数据库,所以如果已经存在就跳过它,这里是代码:
<?php
if(isset($_POST['submit'])){
$ime = $_POST['ime'];
$prezime = $_POST['prezime'];
$email = $_POST['email'];
$adresa = $_POST['adresa'];
$komentar = $_POST['komentar'];
$conn=mysqli_connect('localhost','root','');
mysqli_query($conn,"create database osobni_podatci");
mysqli_select_db($conn,"osobni_podatci");
mysqli_query($conn,"create table podatci(id int primary key auto_increment,ime varchar(50),prezime varchar(50),email varchar(20),adresa varchar(50),komentar varchar(100))");
$conn=mysqli_connect('localhost','root','','osobni_podatci');
$sql="INSERT INTO podatci(ime, prezime,email,adresa,komentar) VALUES('$ime','$prezime','$email','$adresa','$komentar')";
mysqli_query($conn,$sql);
mysqli_close($conn);
echo "Podatci su uspjesno uneseni u bazu podataka 'osobni_podatci'";
}elseif(isset($_POST['brisi'])){
$con = mysqli_connect("localhost","root","","osobni_podatci");
$delete = "DELETE FROM podatci";
mysqli_query($con,$delete);
mysqli_close($con);
echo "Podatci su uspjesno izbrisani iz baze podataka.";
} ?&GT;
答案 0 :(得分:2)
mysqli_query($conn,"create database if not exists osobni_podatci");
答案 1 :(得分:2)
您可以使用
之类的查询mysqli_query($conn,"create database if not exists osobni_podatci");
https://dev.mysql.com/doc/refman/5.0/en/create-database.html
答案 2 :(得分:0)
错误的行$conn=mysqli_connect('localhost','root','');
您必须输入4个参数
首先是主持人,
第二是登录
第三是密码
第四个是数据库名称