Click here to Skip to main content
16,005,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE id = '1'' at line 7


What I have tried:

<?php include 'header.php' ?>

<?php
	$portofolio 	= mysqli_query($conn, "SELECT * FROM portofolio WHERE id = '".$_GET['id']."' ");

	if(mysqli_num_rows($portofolio) == 0){
		echo "<script>window.location='galeri.php'</script>";
	}

	$p 			= mysqli_fetch_object($portofolio);
?>

		<!-- content -->
		<div class="content">

			<div class="container">

				<div class="box">

					<div class="box-header">
						Edit Galeri
					</div>

					<div class="box-body">
						
						<form action="" method="POST" enctype="multipart/form-data">
							<div class="form-group">
								<label>Judul</label>
								<input type="text" name="judul" placeholder="Keterangan" value="<?= $p->judul ?>" class="input-control" required>
							</div>
							<div class="form-group">
								<label>Postingan</label>
								<img src="../uploads/galeri/<?= $p->foto ?>" width="200px" class="image">
								<input type="hidden" name="gambar2" value="<?= $p->foto ?>">
								<input type="file" name="gambar" class="input-control">
							</div>
							<div class="form-group">
								<label>Keterangan</label>
								<input type="text" name="keterangan" placeholder="Keterangan" value="<?= $p->keterangan ?>" class="input-control" required>
							</div>
							<div class="form-group">
								<label>Gambar</label>
								<input type="text" name="gambar" placeholder="Keterangan" value="<?= $p->gambar ?>" class="input-control" required>
							</div>
							<div class="form-group">
								<label>Data</label>
								<input type="date" name="data" placeholder="data" value="<?= $p->data ?>" class="input-control" required>
							</div>

							

							<button type="button" class="btn" onclick="window.location = 'galeri.php'">Kembali</button>
							<input type="submit" name="submit" value="Simpan" class="btn btn-blue">

						</form>

						<?php

							if(isset($_POST['submit'])){

								$judul 	= addslashes(ucwords($_POST['judul']));
								$ket 	= addslashes(ucwords($_POST['keterangan']));
								$gambar 	= addslashes(ucwords($_POST['gambar']));
								$date = date('Y-m-d H:i:s');

								if($_FILES['gambar']['name'] != ''){

									// echo 'user ganti gambar';

									$filename 	= $_FILES['gambar']['name'];
									$tmpname 	= $_FILES['gambar']['tmp_name'];
									$filesize 	= $_FILES['gambar']['size'];

									$formatfile = pathinfo($filename, PATHINFO_EXTENSION);
									$rename 	= 'galeri'.time().'.'.$formatfile;

									$allowedtype = array('png', 'jpg', 'jpeg', 'gif');

									if(!in_array($formatfile, $allowedtype)){

										echo '<div class="alert alert-error">Format file tidak diizinkan.</div>';

										return false;

									}elseif($filesize > 1000000){

										echo '<div class="alert alert-error">Ukuran file tidak boleh lebih dari 1 MB.</div>';

										return false;

									}else{

										if(file_exists("../uploads/galeri/".$_POST['gambar2'])){

											unlink("../uploads/galeri/".$_POST['gambar2']);

										}

										move_uploaded_file($tmpname, "../uploads/galeri/".$rename);

									}

								}else{

									// echo 'user tidak ganti gambar';

									$rename 	= $_POST['gambar2'];

								}

								$update = mysqli_query($conn, "UPDATE portofilio SET
										judul = '".$judul."',
										foto = '".$rename."',
										keterangan = '".$ket."',
										gambar = '".$gambar."',
										data = '".$date."',
										WHERE id = '".$_GET['id']."'
									");


								if($update){
									echo "<script>window.location='galeri.php?success=Edit Data Berhasil'</script>";
								}else{
									echo 'gagal edit '.mysqli_error($conn);
								}

							}

						?>

					</div>

				</div>

			</div>

		</div>

<?php include 'footer.php' ?>
Posted
Updated 29-Dec-21 21:57pm
Comments
Richard MacCutchan 30-Dec-21 4:20am    
You have too many quote characters, as shown in the error message.

1 solution

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that through your whole app, and the problem you have noticed will probably vanish at the same time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900