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
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());
}


?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.