0

I tryed to insert into my db some values from post values and its not working.. Tried just 1 value from 1 post and it works, not working with multiple posts. No errors, nothing :(

http://prntscr.com/67e1qd

code:

$rowSQL = mysql_query( "SELECT MAX( id ) AS max FROM `topic`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];

$new_id = ++$largestNumber;

$newnumber = mysql_query("INSERT INTO topic SET id = '".$new_id."'");

if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){

    $nome = mysql_real_escape_string($_POST['nome']);
    $desc = mysql_real_escape_string($_POST['desc']);
    $link = mysql_real_escape_string($_POST['link']);
    $insert = ("INSERT INTO topic SET nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."' WHERE id = '".$newnumber."'") or die(mysql_error());
    $run = mysql_query($insert);
    if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
    echo "Campos não foram preenchidos.";
}
0

3 Answers 3

3

Try this and post what you're getting:

$rowSQL = mysql_query( "SELECT MAX( id ) AS max FROM `topic`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];

$new_id = ++$largestNumber;

$newnumber = mysql_query("INSERT INTO topic SET id = '".$new_id."'");

if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){

    $nome = mysql_real_escape_string($_POST['nome']);
    $desc = mysql_real_escape_string($_POST['desc']);
    $link = mysql_real_escape_string($_POST['link']);
    $insert = "INSERT INTO topic SET nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."' WHERE id = '".$newnumber."'";
    echo $insert;
    $run = mysql_query($insert) or die(mysql_error());
    if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
    echo "Campos não foram preenchidos.";
}

Edit:

If you want to insert some data then why did you put where clause there?

Edit2:

$update = "UPDATE topic SET nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."' WHERE id = '".$newnumber."'";

$run = mysql_query($update) or die(mysql_error());

Edit3:

$rowSQL = mysql_query( "SELECT MAX( id ) AS max FROM `topic`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];

$new_id = ++$largestNumber;

if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){

    $nome = mysql_real_escape_string($_POST['nome']);
    $desc = mysql_real_escape_string($_POST['desc']);
    $link = mysql_real_escape_string($_POST['link']);
    mysql_query("INSERT INTO topic SET id = '".$new_id."', nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."'") or die(mysql_error());
    if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
    echo "Campos não foram preenchidos.";
}
Sign up to request clarification or add additional context in comments.

10 Comments

INSERT INTO topic SET nome = 'fdgdfgfdgdfg', desc = 'gdfgdfg', link = 'dfgdfgdf' WHERE id = '1'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id = '1'' at line 1
Try: $insert = "INSERT INTO topic SET nome = '".$nome."', desc = '".$desc."', link = '".$link."' ";
Tell me if you want to insert new record or update existsing one
I want to insert the value nome, desc and link to the fields where the id is equal to the one it created
So you want to update exists record, so you need to use UPDATE, look Edit2
|
1

It looks like you're writing code to increment the ID of the row in the database, when you could let MySQL handle this itself. If you set the AUTO INCREMENT true on the id field then all you should need is the following;

// Validate your POST inputs
if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){

$nome = mysql_real_escape_string($_POST['nome']);
$desc = mysql_real_escape_string($_POST['desc']);
$link = mysql_real_escape_string($_POST['link']);

$insert = "INSERT INTO topic (nome, desc, link) VALUES ('".$nome."', '".$desc."', '".$link."')";
echo $insert;
$run = mysql_query($insert) or die(mysql_error());
if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
echo "Campos não foram preenchidos.";
}

If you set the id field to AUTO INCREMENT then the id will automatically set itself to the next number in the list

Comments

0

Try this one:

$rowSQL = mysql_query( "SELECT MAX( id ) AS max FROM `topic`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];

$new_id = ++$largestNumber;

$newnumber = mysql_query("INSERT INTO topic SET id = '".$new_id."'");
$newnumberid = mysql_query("SELECT id FROM topic WHERE id = '".$new_id."'");

if (isset($_POST['submit']) && strlen($_POST['nome'])!== 0 && strlen($_POST['desc'])!== 0 && strlen($_POST['link'])!== 0){

$nome = mysql_real_escape_string($_POST['nome']);
$desc = mysql_real_escape_string($_POST['desc']);
$link = mysql_real_escape_string($_POST['link']);
$insert = "UPDATE topic(nome,desc,link) SET nome = '".$nome."', `desc` = '".$desc."', `link` = '".$link."' WHERE id = '".$newnumberid."'";
echo $insert;
$run = mysql_query($insert) or die(mysql_error());
if($run) { echo '<span style="color:#AFA;text-align:center;margin:8px;">Sucesso!</span>'; } else { echo '<span style="color:#DC143C;text-align:center;margin:8px;">Ocorreu um erro.Tenta novamente.</span>'; }
}else{
echo "Campos não foram preenchidos.";
}

2 Comments

the newnumber is not the problem as you see it on the print, its adding the number. I want to create a new number, and add the input to the fields on that number
still nothing, the same as the answer before

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.