So, I created a form that allows you to add a place, category and image to my database. All this works fine on my local host and correctly inserts data into my database. However, when I post the form on my website, I just get a "error request database". message.
I modified the connection files so that they have the correct information about my server, database name, username and password.
I connect to my database in two different ways. One of them uses mysqli:
<?php
$dbc = mysqli_connect("localhost","root","","ssdb") or die (mysqli_connect_error() );
mysqli_set_charset($dbc, 'utf8');
?>
And also with mysql:
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("ssdb",$con);
?>
In my form, I connect to my database to get the categories you can select using the mysqli function so that it can work?
, mysql php 7+, php 5.6..something.
: http://stephanolsen.dk/imageupload/upload.php
, , :
:
<?php include('header');?>
</head>
<body>
<div class="wrap">
<div class="container">
<div class="page-header">
<h1>Tilføj sted <small><br>Skriv adresse og beskrivelse</small></h1>
</div>
<form id="form" method="post" name="formsub" action="formaction.php" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Titel</label>
<input type="text" name="title" class="form-control" id="title" placeholder="Titel">
</div>
<div class="form-group">
<label for="adresse">Adresse</label>
<input type="text" name="adresse" class="form-control" id="adresse" placeholder="Adresse">
</div>
<div class="form-group">
<label for="beskrivelse">Beskrivelse</label>
<input type="text" name="beskrivelse" class="form-control" id="beskrivelse" placeholder="Beskrivelse">
</div>
<?php
include('includes/connectdb.php');
$query = "SELECT id, name FROM category";
$result_category = mysqli_query($dbc,$query);
?>
<label for="Category">Category</label>
<br />
<?php while($row = mysqli_fetch_array($result_category)): ?>
<input type="checkbox" name="category[]" value=" <?php echo $row['id']; ?> "><?php echo $row['name'] . '<br />'; ?>
<?php endwhile; ?>
<br/>
<input type="file" name="image" />
<br/><br/>
<input type="submit" name="Submit" value="Submit" class="btn btn-default"/>
</form>
</div>
</div>
<br />
</body>
</html>
:
<?php
include('includes/connectdb.php');
$beskrivelse = $_POST['beskrivelse'];
$adresse = $_POST['adresse'];
$title = $_POST['title'];
$query = "INSERT INTO sted (title, beskrivelse, adresse) VALUES ('$title','$beskrivelse', '$adresse')";
mysqli_query($dbc,$query) or die('Error querying database.');
$last_id = mysqli_insert_id($dbc);
echo '<div class="wrap">';
echo '<div class="container">';
echo '<h4>Place has been added!</h4>';
echo '</div>';
echo '</div>';
?>
<?php
include('includes/connect.php');
$checkbox = $_POST['category'];
for ($i=0; $i<sizeof($checkbox);$i++) {
$query = "INSERT INTO placecategory (place_id, category_id) VALUES ('$last_id','".$checkbox[$i]."')";
mysql_query($query) or die(mysql_error());
}
echo "Category is inserted";
?>
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<?php
if(getimagesize($_FILES['image']['tmp_name'])== FALSE)
{
echo "Please select an image.";
}
else
{
$image= addslashes($_FILES['image']['tmp_name']);
$name= addslashes($_FILES['image']['name']);
$place_id=$last_id;
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($name,$image,$place_id);
}
displayimage();
function saveimage($name,$image,$place_id)
{
include('includes/connect.php');
$qry="INSERT INTO pictures (name,image,place_id) values ('$name','$image','$place_id')";
$result=mysql_query($qry,$con);
if($result)
{
}
else
{
}
}
function displayimage()
{
include('includes/connect.php');
$qry="SELECT * FROM pictures";
$result=mysql_query($qry,$con);
while($row = mysql_fetch_array($result))
{
echo '<img height="300" width="300" src="data:image;base64,'.$row['image'].' "> ';
}
mysql_close($con);
}
?>
connectdb.php
<?php
# Connect on localhost for user root
# with password xxxxxxx to database userloginwebsystem
$dbc = mysqli_connect("localhost","root","","ssdb") or die (mysqli_connect_error() );
#Display MySQL version and host
#if(mysqli_ping($dbc))
#{ echo 'MYSQL Server' .mysqli_get_server_info($dbc). ' <br> on ' . mysqli_get_host_info($dbc); }
# Set encoding to match PHP
mysqli_set_charset($dbc, 'utf8');
?>
connect.php
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("ssdb",$con);
?>