Jump to content

would this code work in real situation


redarrow

Recommended Posts

does this code seem workable cheers....

 

 

 

<?php

$x=$_POST['x'];

$database_links=array("google","msn","yahoo","phpfreaks");

foreach($database_links as $x){

echo "<br><a href='http://$x.com'>$x</a><br>";

switch($x){

	case(google);
	$x=$x[0];
	$sql;
	break;

	case(msn);
	$x=$x[1];
	$sql;
    break;
    
	case(yahoo);
	$x=$x[2];
	$sql;
	break;

	case(phpfreaks);
	$x=$x[3];
	$sql;
	break;

}
	$sql="UPDATE counter set count=count+1 WHERE x='$x'";

    $res=mysql_query($sql)or die(mysql_error());
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/92928-would-this-code-work-in-real-situation/
Share on other sites

this should work........

 

 

<?php

//database connection

$x=$_POST['x']; // post x

$database_links=array("google","msn","yahoo","phpfreaks"); //make array

foreach($database_links as $x){ //loop array

echo "<br><a href='http://$x.com'>$x</a><br>";// use array info

switch($x){ // create a switch for varable x

	case(google); // if google goto google in the database and update database with a added 1
	$x=$x[0];
	database($x);
	break;

	case(msn); // if msn goto msn in the database and update database with a added 1
	$x=$x[1];
	database($x);
    break;
    
	case(yahoo); // if yahoo goto yahoo in the database and update database with a added 1
	$x=$x[2];
	database($x);
	break;

	case(phpfreaks); // if phpfreaks goto phpfreaks in the database and update database with a added 1
	$x=$x[3];
	database($x);
	break;
}

}

  function database($x){

	$sql="UPDATE counter set count=count+1 WHERE x='$x'";

    $res=mysql_query($sql)or die(mysql_error());
}


?>

First of all, after each case should come ":" and not a semicolon.

Second, you are using $x twice - to get the post data, and as the value in the foreach. Could lead to unexpected results.

Third, your script updates every single one of the sites (because you go over all of the sites) - you are not using the given post data (that's because you used twice $x and that got you confused).

 

I think what you meant to do is:

 

<?php

$x = $_POST['x'];
$database_links = array("google", "msn", "yahoo", "phpfreaks");

if(in_array($x, database_links))
{
echo "<br><a href='http://www.{$x}.com'>{$x}</a><br>";
database($x);
}

function database($x)
{
$sql="UPDATE counter set count=count+1 WHERE x='$x'";
$res=mysql_query($sql)or die(mysql_error());
}

?>

 

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.