Daftar dan Belajar Sekarang!

Function php 7

foto FitriR
106 Kontribusi 2 Coto
3 bulan yang lalu
Dear Master PHP, Mau bertanya dong pada PHP 5 function di bawah ini dapat berjalan dengan baik. Namun pada saat saya update PHP 7 selalu gagal di eror : "Can\'t insert into hits : "
function addinfo($page5)
{ 
	if(mysqli_num_rows(mysqli_query($link,"SELECT page FROM hits WHERE page = '$page5'")))
	{
	//A counter for this page  already exsists. Now we have to update it.

		$updatecounter = mysqli_query($link,"UPDATE hits SET count = count+1 WHERE page = '$page5'");
		if (!$updatecounter) 
		{
		die ("Can't update the counter : " . mysqli_error($link)); // remove ?
		}	
	} 
	else
	{
	// This page did not exsist in the counter database. A new counter must be created for this page.

		$insert = mysqli_query($link,"INSERT INTO hits (page, count)VALUES ('$page5', '1')");
	
		if (!$insert) 
		{
    		die ("Can\'t insert into hits : " . mysqli_error($link)); // remove ?	
		}
	}
}
Mohon bantuan nya ya master. Terima kasih

Jawaban
foto dianarifr
639 Kontribusi 303 Coto
3 bulan yang lalu
tambahkan global $link didalam fungsi
function addinfo($page5)
{ 
	global $link;
	
	if(mysqli_num_rows(mysqli_query($link,"SELECT page FROM hits WHERE page = '$page5'")))
	{
	//A counter for this page  already exsists. Now we have to update it.

		$updatecounter = mysqli_query($link,"UPDATE hits SET count = count+1 WHERE page = '$page5'");
		if (!$updatecounter) 
		{
		die ("Can't update the counter : " . mysqli_error($link)); // remove ?
		}	
	} 
	else
	{
	// This page did not exsist in the counter database. A new counter must be created for this page.

		$insert = mysqli_query($link,"INSERT INTO hits (page, count)VALUES ('$page5', '1')");
	
		if (!$insert) 
		{
    		die ("Can\'t insert into hits : " . mysqli_error($link)); // remove ?	
		}
	}
}
  • Terima kasih atas masukkan dan saran nya. - FitriR
  • jadi udh solved? - dianarifr
  • sedang di coba-coba.. - FitriR


Login untuk diksusi di forum sekolah koding