heres the working code without validation

<?php // insert.php

// Check whether POST array is populated
if($_POST) {
    // POST array populated, prepare database connection
    $db = mysql_connect('host','user','pass');

    if (!$db)
        die('Could not connect: ' . mysql_error());

    mysql_select_db('mydatabasename', $db);

    // Extract variables from POST array into individual variables
    extract($_POST);
[COLOR="green"]
    // At this stage you should validate that the variables contain what you expect. If not display errors to the user accordingly.[/COLOR]

    // Build the hyperlink. This function will replace each placeholder with the listed variables values respectively
    $hyperlink = sprintf('<html><a href="http://%1$s" target="%2$s">%3$s</a></html>', $url, $target, $showingtext);

    // Build the SQL query. This function will again replace each placeholder with the listed variable values respectively
    $sql = sprintf("INSERT INTO `hyperlinktable` SET `url` = '%s', `target` = '%s', `showingtext` = '%s', `hyperlink` = '%s'",
        $url, $target, $showingtext, $hyperlink);

    // Execute the query
    mysql_query($sql);
}
?>

<html>
    <head>
        <title>Example</title>
    </head>

    <body>
        <?php if($_POST): ?>
            <p>A new link has been created (and was also stored in a table): <?php echo $hyperlink; ?></p>
        <?php endif; ?>

        <form action="insert.php" method="post">
            url: <input type="text" name="url" />
            target: <input type="text" name="target" />
            showingtext: <input type="text" name="showingtext" />
            <input type="submit" />
        </form>
    </body>
</html>

Great. You might find it beneficial to use CODE tags in future, as it makes reading posted source code easier.

Also, if your problem is solved, please mark the thread as so for other users who have a similar problem.

do you have suggestions which editor to use for php code, im using phase ?

id like to use the mysql_fetch_array function to read a urltable than generating an array of hyperlinks using the two other inputs: target,showingtext.

i replaced the $url input with a query of urltable, but the now the array $hyperlinks needs to be stored within the second column of urltable....
this is the part that I'm not sure about `url` = '%s' will it actually put it into the whole column?


$sql = sprintf("INSERT INTO `urltable` SET `url` = '%s', `hyperlink` = '%s'",
$url, $hyperlink);

<?php 

// Check whether POST array is populated
if($_POST) {
// POST array populated, prepare database connection
$db = mysql_connect('host','user','pass');

if (!$db)
die('Could not connect: ' . mysql_error());

mysql_select_db('mydatabasename', $db);


$query = "SELECT * FROM urltable"; 
	 
$result = mysql_query($query) or die(mysql_error());
a

$row = mysql_fetch_array($result) or die(mysql_error());
$url=$row['url']

$hyperlink = sprintf('<html><a href="http://$url" target="%2$s">%3$s</a></html>', $url, $target, $showingtext);

$sql = sprintf("INSERT INTO `hyperlinktable` SET `url` = '%s', `hyperlink` = '%s'",
$url, $hyperlink);

// Execute the query
mysql_query($sql);
}
?>

<html>
<head>
<title>Example</title>
</head>

<body>
<?php if($_POST): ?>
<p>A new link has been created (and was also stored in a table): <?php echo $hyperlink; ?></p>
<?php endif; ?>

<form action="insert.php" method="post">
target: <input type="text" name="target" />
showingtext: <input type="text" name="showingtext" />
<input type="submit" />
</form>
</body>
</html>

I don't understand. What are you trying to do now?

You have mistakenly modified the sprintf function on line 22. And your code will select the first URL from the table and then insert that value everytime.

Perhaps you should consider what your new question is and start a new thread. Less people will read this thread in order to help you now it has been marked as solved.

yes i will start a new thread, unfortunately I was hacked already, so validation i guess should be in there..could you help a bit, i read some stuff about it but still is fairly complicated for me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.