Jump to content

Code not writing to the database.


xXSebaSXx

Recommended Posts

Hello all... new here.  Just now starting to learn PHP and MySQL.

I've started my journey with a this little code:

<?php 
include ("config.php");

function TextBetween($s1,$s2,$s)
{
  $s1 = strtolower($s1);
  $s2 = strtolower($s2);
  $L1 = strlen($s1);
  $scheck = strtolower($s);
  	if($L1>0)
  	{$pos1 = strpos($scheck,$s1);} 
	else 
	{$pos1=0;}
  		if($pos1 !== false)
	  {
   if($s2 == '') return substr($s,$pos1+$L1);
   $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
   if($pos2!==false) return substr($s,$pos1+$L1,$pos2);
  }
  return $result;
}


if ($_POST[submit])
{
$find = array("\n", "\r", "[/url]");
$replace = array("", "", "[/url]\n");
$finalreplace = array("", "", "[/url]");

$urls = $_POST['urls'];
$large = array("[url=", "[img=", "]", "[/url]"); 
$small = array("[url=", "[img=", "]", "[/url]"); 
$urls = str_replace($large, $small, $urls); 
$urls = str_replace($find, $replace, $urls);


for ($i = 0; $i <= sizeof($urls)-1; $i += 1) 
{
$newurl = TextBetween("[url=","][img]",$urls[$i]);
$thumburl = TextBetween("[img=","]",$urls[$i]);

if ($newurl != '')
{
$addtodb = @mysql_query("INSERT INTO redirects (link,thumb) VALUES ('".$newurl."','".$thumburl."'");
$usedid = @mysql_insert_id();
$yoururl = "http://www.sfbyp.com/show.php?rid=".$usedid;
$newurls = $newurls."".trim($urls[$i]);
$newurls = str_replace($newurl, $yoururl, $newurls);
	if ($break == '3')
	{
	$newurls = $newurls."\n";
	$break = 0;
	}
		else
		{
		$break = $break + 1;
		}

}

}
echo "<form action='index.php' method='post' name='mainform'>
<textarea rows='20' cols='90' name='urls' readonly>".$newurls."</textarea>
</form>";
}
else
{
echo "<form action='index.php' method='post' name='mainform'>
<textarea rows='20' cols='90' name='urls'></textarea><br />
<input type='submit' name='submit' id='submit' value='Submit'></form>";
}
?>

 

In my limited knowledge of this matter... I would expect the code to take the data sent via the submission form, process it via the TextBetween() function and write the results from that process to my DB... after that it would display the processed data into the same form...

For some reason... submitting the form doesn't write to the database or display anything afterwards.

Any idea where I may be messing it up?

 

Thanks,

Juan

Link to comment
Share on other sites

Start by removing the @ signs from the mysql_xxxxx() function calls.

 

Then add some error checking to the mysql_query() function call. The examples in the php manual show how to use error checking (or you could read or search the forum as there are thousands of posts where adding error checking to a mysql_query() provided the reason why the query failed.)

Link to comment
Share on other sites

Thank you very much guys...

I think the problem that the script has is that this part is never evaluating to "True".

if ($newurl != '')

 

So that is making the whole DB writhing function get skipped.

I'm sure that the problem stems from here:

 

for ($i = 0; $i <= sizeof($urls)-1; $i += 1) 
{
$newurl = TextBetween("[url=","][img]",$urls[$i]);
$thumburl = TextBetween("[img=","]",$urls[$i]);

 

More specifically the $urls[$i] part.

 

So here is what I wanted to achieve with this script.

 

1.  I submit data in this format image url here.

2.  The scripts cycles though the data organizing so that each BBCODE is in its own separate line.

3.  The organized BBCODES are stored to the $urls variable and converted to lowercase.

 

This part is all OK.... After this... My intention was to have the script do the following:

4.  Cycle through each line in $urls and eliminate the BBCODE tags.  storing the url part to the $newurl variable and the thumb url to the $thumburl variable.

5.  It should then check $newurl, if it's not empty... then it should write the two variables to the respective columns in the DB.

6.  It should then cycle back and continue down the list of BBCODES in $urls.

 

That second part is what is throwing everything off... I guess I'm just gonna have to go back to reading a lot more before I can set this up.

 

Thanks

Juan

 

 

 

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.